Skip to content

Commit

Permalink
JIT: allow forward sub of QMARK nodes (#76476)
Browse files Browse the repository at this point in the history
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 authored Oct 1, 2022
1 parent dc3c200 commit 65a1578
Showing 1 changed file with 36 additions and 6 deletions.
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

0 comments on commit 65a1578

Please sign in to comment.