diff --git a/src/coreclr/jit/assertionprop.cpp b/src/coreclr/jit/assertionprop.cpp index 64c3251a4f2d36..e7065fc8f3c160 100644 --- a/src/coreclr/jit/assertionprop.cpp +++ b/src/coreclr/jit/assertionprop.cpp @@ -3102,18 +3102,7 @@ GenTree* Compiler::optVNBasedFoldConstExpr(BasicBlock* block, GenTree* parent, G // Were able to optimize. conValTree->gtVNPair = vnPair; - GenTree* sideEffList = optExtractSideEffListFromConst(tree); - if (sideEffList != nullptr) - { - // Replace as COMMA(side_effects, const value tree); - assert((sideEffList->gtFlags & GTF_SIDE_EFFECT) != 0); - return gtNewOperNode(GT_COMMA, conValTree->TypeGet(), sideEffList, conValTree); - } - else - { - // No side effects, replace as const value tree. - return conValTree; - } + return gtWrapWithSideEffects(conValTree, tree, GTF_SIDE_EFFECT, true); } else { @@ -6228,52 +6217,6 @@ struct VNAssertionPropVisitorInfo } }; -//------------------------------------------------------------------------------ -// optExtractSideEffListFromConst -// Extracts side effects from a tree so it can be replaced with a comma -// separated list of side effects + a const tree. -// -// Note: -// The caller expects that the root of the tree has no side effects and it -// won't be extracted. Otherwise the resulting comma tree would be bigger -// than the tree before optimization. -// -// Arguments: -// tree - The tree node with constant value to extrace side-effects from. -// -// Return Value: -// 1. Returns the extracted side-effects from "tree" -// 2. When no side-effects are present, returns null. -// -// -GenTree* Compiler::optExtractSideEffListFromConst(GenTree* tree) -{ - assert(vnStore->IsVNConstant(vnStore->VNConservativeNormalValue(tree->gtVNPair))); - - GenTree* sideEffList = nullptr; - - // If we have side effects, extract them. - if ((tree->gtFlags & GTF_SIDE_EFFECT) != 0) - { - // Do a sanity check to ensure persistent side effects aren't discarded and - // tell gtExtractSideEffList to ignore the root of the tree. - // We are relying here on an invariant that VN will only fold non-throwing expressions. - assert(!gtNodeHasSideEffects(tree, GTF_PERSISTENT_SIDE_EFFECTS)); - - // Exception side effects may be ignored because the root is known to be a constant - // (e.g. VN may evaluate a DIV/MOD node to a constant and the node may still - // have GTF_EXCEPT set, even if it does not actually throw any exceptions). - bool ignoreRoot = true; - - gtExtractSideEffList(tree, &sideEffList, GTF_SIDE_EFFECT, ignoreRoot); - - JITDUMP("Extracted side effects from a constant tree [%06u]:\n", tree->gtTreeID); - DISPTREE(sideEffList); - } - - return sideEffList; -} - //------------------------------------------------------------------------------ // optVNConstantPropOnJTrue // Constant propagate on the JTrue node by extracting side effects and moving @@ -6330,7 +6273,8 @@ GenTree* Compiler::optVNConstantPropOnJTrue(BasicBlock* block, GenTree* test) } // Prepare the tree for replacement so any side effects can be extracted. - GenTree* sideEffList = optExtractSideEffListFromConst(relop); + GenTree* sideEffList = nullptr; + gtExtractSideEffList(relop, &sideEffList, GTF_SIDE_EFFECT, true); // Transform the relop's operands to be both zeroes. ValueNum vnZero = vnStore->VNZeroForType(TYP_INT); diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index 29e0cd6fd968c1..f16dc3bccc1c2f 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -8047,7 +8047,6 @@ class Compiler GenTree* optVNBasedFoldExpr(BasicBlock* block, GenTree* parent, GenTree* tree); GenTree* optVNBasedFoldExpr_Call(BasicBlock* block, GenTree* parent, GenTreeCall* call); GenTree* optVNBasedFoldExpr_Call_Memmove(GenTreeCall* call); - GenTree* optExtractSideEffListFromConst(GenTree* tree); AssertionIndex GetAssertionCount() { diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 225e7eccd93860..3fd5955465739e 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -17223,7 +17223,13 @@ GenTree* Compiler::gtWrapWithSideEffects(GenTree* tree, // It should be possible to be smarter here and allow such cases by extracting the side effects // properly for this particular case. For now, caller is responsible for avoiding such cases. - tree = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree); + GenTree* comma = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree); + if ((vnStore != nullptr) && tree->gtVNPair.BothDefined() && sideEffectsSource->gtVNPair.BothDefined()) + { + comma->gtVNPair = + vnStore->VNPWithExc(tree->gtVNPair, vnStore->VNPExceptionSet(sideEffectsSource->gtVNPair)); + } + return comma; } return tree; }