Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: allow forward sub of QMARK nodes #76476

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
JIT: allow forward sub of QMARK nodes
There is often a single-def single use temp consuming a QMARK (which in
turn often comes from expansion of `isinst` and friends). This temp
assignment tends to stay around and can inhibit redundant branch opts
in a number of interesting cases. It may also serve as an attractive
nuisance for copy prop.

While it would be ideal to generalize RBO to handle assignment side
effects, doing so appears quite challenging, as we would need to rewrite
possibly large chunks of the SSA and VN graphs. Forward sub eliminates
the temp and so clears the way for the existing RBO to remove more branches.

Addresses aspects of the "side effect" enhancements for RBO (see #48115).
  • Loading branch information
AndyAyersMS committed Oct 1, 2022
commit 4cdb8a4eb3c3c720e4ab12f8f13f51881594756d
42 changes: 36 additions & 6 deletions src/coreclr/jit/forwardsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,13 @@ bool Compiler::fgForwardSubStatement(Statement* stmt)
GenTree* const rhsNode = rootNode->gtGetOp2();
GenTree* fwdSubNode = rhsNode;

// Can't substitute a qmark (unless the use is RHS of an assign... could check for this)
// Can't substitute GT_CATCH_ARG.
// Can't substitute GT_LCLHEAP.
//
// Don't substitute a no return call (trips up morph in some cases).
//
if (fwdSubNode->OperIs(GT_QMARK, GT_CATCH_ARG, GT_LCLHEAP))
if (fwdSubNode->OperIs(GT_CATCH_ARG, GT_LCLHEAP))
{
JITDUMP(" tree to sub is qmark, catch arg, or lcl heap\n");
JITDUMP(" tree to sub is catch arg, or lcl heap\n");
return false;
}

Expand Down Expand Up @@ -511,6 +509,40 @@ bool Compiler::fgForwardSubStatement(Statement* stmt)

JITDUMP(" [%06u] is only use of [%06u] (V%02u) ", dspTreeID(fsv.GetNode()), dspTreeID(lhsNode), lclNum);

// Qmarks must replace top-level uses. Also, restrict to GT_ASG.
// And also to where neither local is normalize on store, otherwise
// something downstream may add a cast over the qmark.
//
GenTree* const nextRootNode = nextStmt->GetRootNode();
if (fwdSubNode->OperIs(GT_QMARK))
{
if ((fsv.GetParentNode() != nextRootNode) || !nextRootNode->OperIs(GT_ASG))
{
JITDUMP(" can't fwd sub qmark as use is not top level ASG\n");
return false;
}

if (varDsc->lvNormalizeOnStore())
{
JITDUMP(" can't fwd sub qmark as V%02u is normalize on store\n", lclNum);
return false;
}

GenTree* const nextRootNodeLHS = nextRootNode->gtGetOp1();

if (nextRootNodeLHS->OperIs(GT_LCL_VAR))
{
const unsigned lhsLclNum = nextRootNodeLHS->AsLclVarCommon()->GetLclNum();
LclVarDsc* const lhsVarDsc = lvaGetDesc(lhsLclNum);

if (lhsVarDsc->lvNormalizeOnStore())
{
JITDUMP(" can't fwd sub qmark as V%02u is normalize on store\n", lhsLclNum);
return false;
}
}
}

// If next statement already has a large tree, hold off
// on making it even larger.
//
Expand All @@ -527,8 +559,6 @@ bool Compiler::fgForwardSubStatement(Statement* stmt)
// Next statement seems suitable.
// See if we can forward sub without changing semantics.
//
GenTree* const nextRootNode = nextStmt->GetRootNode();

// Bail if types disagree.
// Might be able to tolerate these by retyping.
//
Expand Down