@@ -4903,7 +4903,6 @@ static void SetIndirectStoreEvalOrder(Compiler* comp, GenTreeIndir* store, bool*
49034903 * 1. GetCostEx() to the execution complexity estimate
49044904 * 2. GetCostSz() to the code size estimate
49054905 * 3. Sometimes sets GTF_ADDRMODE_NO_CSE on nodes in the tree.
4906- * 4. DEBUG-only: clears GTF_DEBUG_NODE_MORPHED.
49074906 */
49084907
49094908#ifdef _PREFAST_
@@ -14388,10 +14387,7 @@ GenTree* Compiler::gtFoldExprSpecial(GenTree* tree)
1438814387 // Helper function that creates a new IntCon node and morphs it, if required
1438914388 auto NewMorphedIntConNode = [&](int value) -> GenTreeIntCon* {
1439014389 GenTreeIntCon* icon = gtNewIconNode(value);
14391- if (fgGlobalMorph)
14392- {
14393- INDEBUG(icon->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
14394- }
14390+ icon->SetMorphed(this);
1439514391 return icon;
1439614392 };
1439714393
@@ -14402,18 +14398,14 @@ GenTree* Compiler::gtFoldExprSpecial(GenTree* tree)
1440214398 assert(varTypeIsUnsigned(castToType));
1440314399
1440414400 GenTreeCast* cast = gtNewCastNode(TYP_INT, op1, false, castToType);
14405- if (fgGlobalMorph)
14406- {
14407- fgMorphTreeDone(cast);
14408- }
14401+ cast->SetMorphed(this);
14402+ fgMorphTreeDone(cast);
1440914403
1441014404 if (type == TYP_LONG)
1441114405 {
1441214406 cast = gtNewCastNode(TYP_LONG, cast, true, TYP_LONG);
14413- if (fgGlobalMorph)
14414- {
14415- fgMorphTreeDone(cast);
14416- }
14407+ cast->SetMorphed(this);
14408+ fgMorphTreeDone(cast);
1441714409 }
1441814410
1441914411 return cast;
@@ -14680,14 +14672,7 @@ GenTree* Compiler::gtFoldExprSpecial(GenTree* tree)
1468014672 DISPTREE(tree);
1468114673 JITDUMP("Transformed into:\n");
1468214674 DISPTREE(op);
14683-
14684- if (fgGlobalMorph)
14685- {
14686- // We can sometimes produce a comma over the constant if the original op
14687- // had a side effect, so just ensure we set the flag (which will be already
14688- // set for the operands otherwise).
14689- INDEBUG(op->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
14690- }
14675+ op->SetMorphed(this);
1469114676 return op;
1469214677}
1469314678
@@ -14742,10 +14727,9 @@ GenTree* Compiler::gtFoldExprSpecialFloating(GenTree* tree)
1474214727 // Helper function that creates a new IntCon node and morphs it, if required
1474314728 auto NewMorphedIntConNode = [&](int value) -> GenTreeIntCon* {
1474414729 GenTreeIntCon* icon = gtNewIconNode(value);
14745- if (fgGlobalMorph)
14746- {
14747- INDEBUG(icon->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
14748- }
14730+
14731+ icon->SetMorphed(this);
14732+
1474914733 return icon;
1475014734 };
1475114735
@@ -14907,14 +14891,8 @@ GenTree* Compiler::gtFoldExprSpecialFloating(GenTree* tree)
1490714891 DISPTREE(tree);
1490814892 JITDUMP("Transformed into:\n");
1490914893 DISPTREE(op);
14894+ op->SetMorphed(this);
1491014895
14911- if (fgGlobalMorph)
14912- {
14913- // We can sometimes produce a comma over the constant if the original op
14914- // had a side effect, so just ensure we set the flag (which will be already
14915- // set for the operands otherwise).
14916- INDEBUG(op->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
14917- }
1491814896 return op;
1491914897}
1492014898
@@ -15435,6 +15413,11 @@ GenTree* Compiler::gtOptimizeEnumHasFlag(GenTree* thisOp, GenTree* flagOp)
1543515413 Statement* thisStoreStmt = thisOp->AsBox()->gtCopyStmtWhenInlinedBoxValue;
1543615414 thisStoreStmt->SetRootNode(thisStore);
1543715415 thisValOpt = gtNewLclvNode(thisTmp, type);
15416+
15417+ // If this is invoked during global morph we are adding code to a remote tree
15418+ // Despite this being a store, we can't meaningfully add assertions
15419+ //
15420+ thisStore->SetMorphed(this);
1543815421 }
1543915422
1544015423 if (flagVal->IsIntegralConst())
@@ -15452,6 +15435,11 @@ GenTree* Compiler::gtOptimizeEnumHasFlag(GenTree* thisOp, GenTree* flagOp)
1545215435 flagStoreStmt->SetRootNode(flagStore);
1545315436 flagValOpt = gtNewLclvNode(flagTmp, type);
1545415437 flagValOptCopy = gtNewLclvNode(flagTmp, type);
15438+
15439+ // If this is invoked during global morph we are adding code to a remote tree
15440+ // Despite this being a store, we can't meaningfully add assertions
15441+ //
15442+ flagStore->SetMorphed(this);
1545515443 }
1545615444
1545715445 // Turn the call into (thisValTmp & flagTmp) == flagTmp.
@@ -17436,16 +17424,7 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
1743617424 }
1743717425
1743817426 GenTree* comma = m_compiler->gtNewOperNode(GT_COMMA, TYP_VOID, m_result, node);
17439-
17440- #ifdef DEBUG
17441- if (m_compiler->fgGlobalMorph)
17442- {
17443- // Either both should be morphed or neither should be.
17444- assert((m_result->gtDebugFlags & GTF_DEBUG_NODE_MORPHED) ==
17445- (node->gtDebugFlags & GTF_DEBUG_NODE_MORPHED));
17446- comma->gtDebugFlags |= node->gtDebugFlags & GTF_DEBUG_NODE_MORPHED;
17447- }
17448- #endif
17427+ comma->SetMorphed(m_compiler);
1744917428
1745017429 // Both should have valuenumbers defined for both or for neither
1745117430 // one (unless we are remorphing, in which case a prior transform
@@ -30762,12 +30741,7 @@ GenTree* Compiler::gtFoldExprHWIntrinsic(GenTreeHWIntrinsic* tree)
3076230741 }
3076330742
3076430743 vecCon->gtSimdVal = simdVal;
30765-
30766- if (fgGlobalMorph)
30767- {
30768- INDEBUG(vecCon->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
30769- }
30770-
30744+ vecCon->SetMorphed(this);
3077130745 fgUpdateConstTreeValueNumber(vecCon);
3077230746 return vecCon;
3077330747 }
@@ -30829,11 +30803,7 @@ GenTree* Compiler::gtFoldExprHWIntrinsic(GenTreeHWIntrinsic* tree)
3082930803#endif // !TARGET_XARCH && !TARGET_ARM64
3083030804
3083130805 DEBUG_DESTROY_NODE(op, tree);
30832-
30833- if (fgGlobalMorph)
30834- {
30835- INDEBUG(vectorNode->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
30836- }
30806+ vectorNode->SetMorphed(this);
3083730807 return vectorNode;
3083830808 }
3083930809 }
@@ -32018,17 +31988,10 @@ GenTree* Compiler::gtFoldExprHWIntrinsic(GenTreeHWIntrinsic* tree)
3201831988
3201931989 if (resultNode != tree)
3202031990 {
32021- if (fgGlobalMorph)
31991+ resultNode->SetMorphed(this);
31992+ if (resultNode->OperIs(GT_COMMA))
3202231993 {
32023- // We can sometimes produce a comma over the constant if the original op
32024- // had a side effect or even a new constant node, so just ensure we set
32025- // the flag (which will be already set for the operands otherwise).
32026- INDEBUG(resultNode->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
32027-
32028- if (resultNode->OperIs(GT_COMMA))
32029- {
32030- INDEBUG(resultNode->AsOp()->gtGetOp2()->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
32031- }
31994+ resultNode->AsOp()->gtGetOp2()->SetMorphed(this);
3203231995 }
3203331996
3203431997 if (resultNode->OperIsConst())
@@ -32155,3 +32118,65 @@ bool Compiler::gtCanSkipCovariantStoreCheck(GenTree* value, GenTree* array)
3215532118
3215632119 return false;
3215732120}
32121+
32122+ #if defined(DEBUG)
32123+ //------------------------------------------------------------------------
32124+ // SetMorphed: mark a node as having been morphed
32125+ //
32126+ // Arguments:
32127+ // compiler - compiler instance
32128+ // doChildren - recursive mark child nodes
32129+ //
32130+ // Notes:
32131+ // Does nothing outside of global morph.
32132+ //
32133+ // Useful for morph post-order expansions / optimizations.
32134+ //
32135+ // Use care when invoking this on an assignment (or when doChildren is true,
32136+ // on trees containing assignments) as those usually will also require
32137+ // local assertion updates.
32138+ //
32139+ void GenTree::SetMorphed(Compiler* compiler, bool doChildren /* = false */)
32140+ {
32141+ if (!compiler->fgGlobalMorph)
32142+ {
32143+ return;
32144+ }
32145+
32146+ struct Visitor : GenTreeVisitor<Visitor>
32147+ {
32148+ enum
32149+ {
32150+ DoPostOrder = true,
32151+ };
32152+
32153+ Visitor(Compiler* comp)
32154+ : GenTreeVisitor(comp)
32155+ {
32156+ }
32157+
32158+ fgWalkResult PostOrderVisit(GenTree** use, GenTree* user)
32159+ {
32160+ GenTree* const node = *use;
32161+ if (!node->WasMorphed())
32162+ {
32163+ node->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED;
32164+ node->gtMorphCount++;
32165+ }
32166+ return Compiler::WALK_CONTINUE;
32167+ }
32168+ };
32169+
32170+ if (doChildren)
32171+ {
32172+ Visitor v(compiler);
32173+ GenTree* node = this;
32174+ v.WalkTree(&node, nullptr);
32175+ }
32176+ else if (!WasMorphed())
32177+ {
32178+ gtDebugFlags |= GTF_DEBUG_NODE_MORPHED;
32179+ gtMorphCount++;
32180+ }
32181+ }
32182+ #endif
0 commit comments