Skip to content

Commit

Permalink
Assign VN for COMMA in gtWrapWithSideEffects (#108955)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Oct 17, 2024
1 parent 1b8f744 commit 07e9313
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 61 deletions.
62 changes: 3 additions & 59 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8054,7 +8054,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()
{
Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17227,7 +17227,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;
}
Expand Down

0 comments on commit 07e9313

Please sign in to comment.