Skip to content

Commit 07e9313

Browse files
authored
Assign VN for COMMA in gtWrapWithSideEffects (#108955)
1 parent 1b8f744 commit 07e9313

3 files changed

Lines changed: 10 additions & 61 deletions

File tree

src/coreclr/jit/assertionprop.cpp

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,18 +3102,7 @@ GenTree* Compiler::optVNBasedFoldConstExpr(BasicBlock* block, GenTree* parent, G
31023102

31033103
// Were able to optimize.
31043104
conValTree->gtVNPair = vnPair;
3105-
GenTree* sideEffList = optExtractSideEffListFromConst(tree);
3106-
if (sideEffList != nullptr)
3107-
{
3108-
// Replace as COMMA(side_effects, const value tree);
3109-
assert((sideEffList->gtFlags & GTF_SIDE_EFFECT) != 0);
3110-
return gtNewOperNode(GT_COMMA, conValTree->TypeGet(), sideEffList, conValTree);
3111-
}
3112-
else
3113-
{
3114-
// No side effects, replace as const value tree.
3115-
return conValTree;
3116-
}
3105+
return gtWrapWithSideEffects(conValTree, tree, GTF_SIDE_EFFECT, true);
31173106
}
31183107
else
31193108
{
@@ -6228,52 +6217,6 @@ struct VNAssertionPropVisitorInfo
62286217
}
62296218
};
62306219

6231-
//------------------------------------------------------------------------------
6232-
// optExtractSideEffListFromConst
6233-
// Extracts side effects from a tree so it can be replaced with a comma
6234-
// separated list of side effects + a const tree.
6235-
//
6236-
// Note:
6237-
// The caller expects that the root of the tree has no side effects and it
6238-
// won't be extracted. Otherwise the resulting comma tree would be bigger
6239-
// than the tree before optimization.
6240-
//
6241-
// Arguments:
6242-
// tree - The tree node with constant value to extrace side-effects from.
6243-
//
6244-
// Return Value:
6245-
// 1. Returns the extracted side-effects from "tree"
6246-
// 2. When no side-effects are present, returns null.
6247-
//
6248-
//
6249-
GenTree* Compiler::optExtractSideEffListFromConst(GenTree* tree)
6250-
{
6251-
assert(vnStore->IsVNConstant(vnStore->VNConservativeNormalValue(tree->gtVNPair)));
6252-
6253-
GenTree* sideEffList = nullptr;
6254-
6255-
// If we have side effects, extract them.
6256-
if ((tree->gtFlags & GTF_SIDE_EFFECT) != 0)
6257-
{
6258-
// Do a sanity check to ensure persistent side effects aren't discarded and
6259-
// tell gtExtractSideEffList to ignore the root of the tree.
6260-
// We are relying here on an invariant that VN will only fold non-throwing expressions.
6261-
assert(!gtNodeHasSideEffects(tree, GTF_PERSISTENT_SIDE_EFFECTS));
6262-
6263-
// Exception side effects may be ignored because the root is known to be a constant
6264-
// (e.g. VN may evaluate a DIV/MOD node to a constant and the node may still
6265-
// have GTF_EXCEPT set, even if it does not actually throw any exceptions).
6266-
bool ignoreRoot = true;
6267-
6268-
gtExtractSideEffList(tree, &sideEffList, GTF_SIDE_EFFECT, ignoreRoot);
6269-
6270-
JITDUMP("Extracted side effects from a constant tree [%06u]:\n", tree->gtTreeID);
6271-
DISPTREE(sideEffList);
6272-
}
6273-
6274-
return sideEffList;
6275-
}
6276-
62776220
//------------------------------------------------------------------------------
62786221
// optVNConstantPropOnJTrue
62796222
// Constant propagate on the JTrue node by extracting side effects and moving
@@ -6330,7 +6273,8 @@ GenTree* Compiler::optVNConstantPropOnJTrue(BasicBlock* block, GenTree* test)
63306273
}
63316274

63326275
// Prepare the tree for replacement so any side effects can be extracted.
6333-
GenTree* sideEffList = optExtractSideEffListFromConst(relop);
6276+
GenTree* sideEffList = nullptr;
6277+
gtExtractSideEffList(relop, &sideEffList, GTF_SIDE_EFFECT, true);
63346278

63356279
// Transform the relop's operands to be both zeroes.
63366280
ValueNum vnZero = vnStore->VNZeroForType(TYP_INT);

src/coreclr/jit/compiler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8054,7 +8054,6 @@ class Compiler
80548054
GenTree* optVNBasedFoldExpr(BasicBlock* block, GenTree* parent, GenTree* tree);
80558055
GenTree* optVNBasedFoldExpr_Call(BasicBlock* block, GenTree* parent, GenTreeCall* call);
80568056
GenTree* optVNBasedFoldExpr_Call_Memmove(GenTreeCall* call);
8057-
GenTree* optExtractSideEffListFromConst(GenTree* tree);
80588057

80598058
AssertionIndex GetAssertionCount()
80608059
{

src/coreclr/jit/gentree.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17227,7 +17227,13 @@ GenTree* Compiler::gtWrapWithSideEffects(GenTree* tree,
1722717227
// It should be possible to be smarter here and allow such cases by extracting the side effects
1722817228
// properly for this particular case. For now, caller is responsible for avoiding such cases.
1722917229

17230-
tree = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree);
17230+
GenTree* comma = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree);
17231+
if ((vnStore != nullptr) && tree->gtVNPair.BothDefined() && sideEffectsSource->gtVNPair.BothDefined())
17232+
{
17233+
comma->gtVNPair =
17234+
vnStore->VNPWithExc(tree->gtVNPair, vnStore->VNPExceptionSet(sideEffectsSource->gtVNPair));
17235+
}
17236+
return comma;
1723117237
}
1723217238
return tree;
1723317239
}

0 commit comments

Comments
 (0)