From 7779243281f82d6e73bd8b98cba84e934194509a Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Thu, 12 Dec 2024 17:07:11 +0100 Subject: [PATCH] JIT: Avoid remorphing general `gtFoldExpr` return --- src/coreclr/jit/gentree.cpp | 24 +++++++++--------------- src/coreclr/jit/morph.cpp | 24 +----------------------- 2 files changed, 10 insertions(+), 38 deletions(-) diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index d8d5752a5f6441..216b7a81785c63 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -16571,7 +16571,9 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree) // update args table. For this reason this optimization is enabled only // for global morphing phase. // - // TODO-CQ: Once fgMorphArgs() is fixed this restriction could be removed. + // TODO-CQ: Once fgMorphArgs() is fixed this restriction could be removed, + // but it would require to know whether or not we are after morph such that + // we know when the call created below needs to be in morphed form. if (!fgGlobalMorph) { @@ -16581,10 +16583,10 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree) var_types type = genActualType(tree->TypeGet()); op1 = type == TYP_LONG ? gtNewLconNode(0) : gtNewIconNode(0); - if (vnStore != nullptr) - { - op1->gtVNPair.SetBoth(vnStore->VNZeroForType(type)); - } + INDEBUG(op1->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED); + + // Would need to set VNs if this wasn't in global morph only. + assert(vnStore == nullptr); JITDUMP("\nFolding binary operator with constant nodes into a comma throw:\n"); DISPTREE(tree); @@ -16598,18 +16600,10 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree) op2 = op1; op1 = gtNewHelperCallNode(CORINFO_HELP_OVERFLOW, TYP_VOID, gtNewIconNode(compCurBB->bbTryIndex)); - - // op1 is a call to the JIT helper that throws an Overflow exception. - // Attach the ExcSet for VNF_OverflowExc(Void) to this call. - - if (vnStore != nullptr) - { - op1->gtVNPair = vnStore->VNPWithExc(ValueNumPair(ValueNumStore::VNForVoid(), ValueNumStore::VNForVoid()), - vnStore->VNPExcSetSingleton(vnStore->VNPairForFunc(TYP_REF, VNF_OverflowExc, - vnStore->VNPForVoid()))); - } + op1 = fgMorphTree(op1); tree = gtNewOperNode(GT_COMMA, tree->TypeGet(), op1, op2); + INDEBUG(tree->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED); return tree; } diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index c0b7a6417b2980..e5237dd88b3fea 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -8307,29 +8307,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA // Try to fold it, maybe we get lucky, tree = gtFoldExpr(tree); - if (oldTree != tree) - { - /* if gtFoldExpr returned op1 or op2 then we are done */ - if ((tree == op1) || (tree == op2) || (tree == qmarkOp1) || (tree == qmarkOp2)) - { - return tree; - } - - /* If we created a comma-throw tree then we need to morph op1 */ - if (fgIsCommaThrow(tree)) - { - tree->AsOp()->gtOp1 = fgMorphTree(tree->AsOp()->gtOp1); - fgMorphTreeDone(tree); - return tree; - } - - return tree; - } - else if (tree->OperIsConst()) - { - return tree; - } - else if (tree->IsNothingNode()) + if ((oldTree != tree) || tree->OperIsConst() || tree->IsNothingNode()) { return tree; }