From 944bbe448173082b907972e0bd01ffd57e10e5c3 Mon Sep 17 00:00:00 2001 From: Sergey Andreenko Date: Wed, 11 Oct 2017 18:28:35 -0700 Subject: [PATCH] Refactor CopyFrom --- src/jit/compiler.hpp | 24 ++++++++++++++++++------ src/jit/earlyprop.cpp | 2 +- src/jit/flowgraph.cpp | 12 ++++++------ src/jit/gentree.h | 2 +- src/jit/morph.cpp | 6 +++--- src/jit/stackfp.cpp | 2 +- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/jit/compiler.hpp b/src/jit/compiler.hpp index f357e6f..7263718 100644 --- a/src/jit/compiler.hpp +++ b/src/jit/compiler.hpp @@ -1456,20 +1456,31 @@ inline void GenTree::SetOper(genTreeOps oper, ValueNumberUpdate vnUpdate) } } -inline void GenTree::CopyFrom(const GenTree* src, Compiler* comp) +//------------------------------------------------------------------------ +// ReplaceWith: replace this with the src node. The source must be an isolated node +// and cannot be used after the replacement. +// +// Arguments: +// src - source tree, that replaces this. +// comp - the compiler instance to transfer annotations for arrays. +// +inline void GenTree::ReplaceWith(GenTree* src, Compiler* comp) { - /* The source may be big only if the target is also a big node */ - + // The source may be big only if the target is also a big node assert((gtDebugFlags & GTF_DEBUG_NODE_LARGE) || GenTree::s_gtNodeSizes[src->gtOper] == TREE_NODE_SZ_SMALL); - GenTreePtr prev = gtPrev; - GenTreePtr next = gtNext; + + // The check is effective only if nodes have been already threaded. + assert((src->gtPrev == nullptr) && (src->gtNext == nullptr)); RecordOperBashing(OperGet(), src->OperGet()); // nop unless NODEBASH_STATS is enabled + GenTreePtr prev = gtPrev; + GenTreePtr next = gtNext; // The VTable pointer is copied intentionally here memcpy((void*)this, (void*)src, src->GetNodeSize()); this->gtPrev = prev; this->gtNext = next; + #ifdef DEBUG gtSeqNum = 0; #endif @@ -1481,6 +1492,7 @@ inline void GenTree::CopyFrom(const GenTree* src, Compiler* comp) assert(b); comp->GetArrayInfoMap()->Set(this, arrInfo); } + DEBUG_DESTROY_NODE(src); } inline GenTreePtr Compiler::gtNewCastNode(var_types typ, GenTreePtr op1, var_types castType) @@ -1521,7 +1533,7 @@ inline void GenTree::SetOper(genTreeOps oper, ValueNumberUpdate vnUpdate) } } -inline void GenTree::CopyFrom(GenTreePtr src) +inline void GenTree::ReplaceWith(GenTreePtr src) { RecordOperBashing(OperGet(), src->OperGet()); // nop unless NODEBASH_STATS is enabled *this = *src; diff --git a/src/jit/earlyprop.cpp b/src/jit/earlyprop.cpp index 7e9b6d1..b0cb26b 100644 --- a/src/jit/earlyprop.cpp +++ b/src/jit/earlyprop.cpp @@ -363,7 +363,7 @@ GenTreePtr Compiler::optEarlyPropRewriteTree(GenTreePtr tree) DecLclVarRefCountsVisitor::WalkTree(this, tree); // acutalValClone has small tree node size, it is safe to use CopyFrom here. - tree->CopyFrom(actualValClone, this); + tree->ReplaceWith(actualValClone, this); IncLclVarRefCountsVisitor::WalkTree(this, tree); #ifdef DEBUG diff --git a/src/jit/flowgraph.cpp b/src/jit/flowgraph.cpp index 15e8d97..69c8326 100644 --- a/src/jit/flowgraph.cpp +++ b/src/jit/flowgraph.cpp @@ -22214,7 +22214,7 @@ void Compiler::fgAttachStructInlineeToAsg(GenTreePtr tree, GenTreePtr child, COR ? fgAssignStructInlineeToVar(child, retClsHnd) // Assign to a variable if it is a call. : child); // Just get the address, if not a call. - tree->CopyFrom(gtNewCpObjNode(dstAddr, srcAddr, retClsHnd, false), this); + tree->ReplaceWith(gtNewCpObjNode(dstAddr, srcAddr, retClsHnd, false), this); } #endif // FEATURE_MULTIREG_RET @@ -22288,12 +22288,12 @@ Compiler::fgWalkResult Compiler::fgUpdateInlineReturnExpressionPlaceHolder(GenTr printf(" with "); printTreeID(inlineCandidate); printf("\n"); - // Dump out the old return expression placeholder it will be overwritten by the CopyFrom below + // Dump out the old return expression placeholder it will be overwritten by the ReplaceWith below comp->gtDispTree(tree); } #endif // DEBUG - tree->CopyFrom(inlineCandidate, comp); + tree->ReplaceWith(inlineCandidate, comp); #ifdef DEBUG if (comp->verbose) @@ -22364,7 +22364,7 @@ Compiler::fgWalkResult Compiler::fgUpdateInlineReturnExpressionPlaceHolder(GenTr else { // Just assign the inlinee to a variable to keep it simple. - tree->CopyFrom(comp->fgAssignStructInlineeToVar(tree, retClsHnd), comp); + tree->ReplaceWith(comp->fgAssignStructInlineeToVar(tree, retClsHnd), comp); } } } @@ -22994,7 +22994,7 @@ _Done: } #endif // DEBUG // Replace the call with the return expression - iciCall->CopyFrom(pInlineInfo->retExpr, this); + iciCall->ReplaceWith(pInlineInfo->retExpr, this); } // @@ -23114,7 +23114,7 @@ GenTreePtr Compiler::fgInlinePrependStatements(InlineInfo* inlineInfo) // Change the temp in-place to the actual argument. // We currently do not support this for struct arguments, so it must not be a GT_OBJ. assert(argNode->gtOper != GT_OBJ); - argSingleUseNode->CopyFrom(argNode, this); + argSingleUseNode->ReplaceWith(argNode, this); continue; } else diff --git a/src/jit/gentree.h b/src/jit/gentree.h index 37deba9..3443634 100644 --- a/src/jit/gentree.h +++ b/src/jit/gentree.h @@ -1822,7 +1822,7 @@ public: bool IsNodeProperlySized() const; - void CopyFrom(const GenTree* src, Compiler* comp); + void ReplaceWith(GenTree* src, Compiler* comp); static genTreeOps ReverseRelop(genTreeOps relop); diff --git a/src/jit/morph.cpp b/src/jit/morph.cpp index 2b0e33f..ad4080d 100644 --- a/src/jit/morph.cpp +++ b/src/jit/morph.cpp @@ -15482,10 +15482,10 @@ GenTreePtr Compiler::fgMorphTree(GenTreePtr tree, MorphAddrContext* mac) copy = new (this, GT_CALL) GenTreeCall(TYP_INT); } - copy->CopyFrom(tree, this); + copy->ReplaceWith(tree, this); #if defined(LATE_DISASM) - // GT_CNS_INT is considered small, so CopyFrom() won't copy all fields + // GT_CNS_INT is considered small, so ReplaceWith() won't copy all fields if ((tree->gtOper == GT_CNS_INT) && tree->IsIconHandle()) { copy->gtIntCon.gtIconHdl.gtIconHdl1 = tree->gtIntCon.gtIconHdl.gtIconHdl1; @@ -18757,7 +18757,7 @@ GenTreePtr Compiler::fgMorphImplicitByRefArgs(GenTreePtr tree, bool isAddr) if (fieldHnd == nullptr) { // change &X into just plain X - tree->CopyFrom(lclVarTree, this); + tree->ReplaceWith(lclVarTree, this); tree->gtType = TYP_BYREF; } else diff --git a/src/jit/stackfp.cpp b/src/jit/stackfp.cpp index 594c73e..afdec7e 100644 --- a/src/jit/stackfp.cpp +++ b/src/jit/stackfp.cpp @@ -3281,7 +3281,7 @@ GenTreePtr CodeGen::genMakeAddressableStackFP(GenTreePtr tree, printf(" with value %lf\n", tree->gtDblCon.gtDconVal); } #endif // DEBUG - tree->CopyFrom(addr, compiler); + tree->ReplaceWith(addr, compiler); return tree; } break; -- 2.7.4