Skip to content

Commit 65a1578

Browse files
authored
JIT: allow forward sub of QMARK nodes (#76476)
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).
1 parent dc3c200 commit 65a1578

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/coreclr/jit/forwardsub.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,13 @@ bool Compiler::fgForwardSubStatement(Statement* stmt)
434434
GenTree* const rhsNode = rootNode->gtGetOp2();
435435
GenTree* fwdSubNode = rhsNode;
436436

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

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

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

512+
// Qmarks must replace top-level uses. Also, restrict to GT_ASG.
513+
// And also to where neither local is normalize on store, otherwise
514+
// something downstream may add a cast over the qmark.
515+
//
516+
GenTree* const nextRootNode = nextStmt->GetRootNode();
517+
if (fwdSubNode->OperIs(GT_QMARK))
518+
{
519+
if ((fsv.GetParentNode() != nextRootNode) || !nextRootNode->OperIs(GT_ASG))
520+
{
521+
JITDUMP(" can't fwd sub qmark as use is not top level ASG\n");
522+
return false;
523+
}
524+
525+
if (varDsc->lvNormalizeOnStore())
526+
{
527+
JITDUMP(" can't fwd sub qmark as V%02u is normalize on store\n", lclNum);
528+
return false;
529+
}
530+
531+
GenTree* const nextRootNodeLHS = nextRootNode->gtGetOp1();
532+
533+
if (nextRootNodeLHS->OperIs(GT_LCL_VAR))
534+
{
535+
const unsigned lhsLclNum = nextRootNodeLHS->AsLclVarCommon()->GetLclNum();
536+
LclVarDsc* const lhsVarDsc = lvaGetDesc(lhsLclNum);
537+
538+
if (lhsVarDsc->lvNormalizeOnStore())
539+
{
540+
JITDUMP(" can't fwd sub qmark as V%02u is normalize on store\n", lhsLclNum);
541+
return false;
542+
}
543+
}
544+
}
545+
514546
// If next statement already has a large tree, hold off
515547
// on making it even larger.
516548
//
@@ -527,8 +559,6 @@ bool Compiler::fgForwardSubStatement(Statement* stmt)
527559
// Next statement seems suitable.
528560
// See if we can forward sub without changing semantics.
529561
//
530-
GenTree* const nextRootNode = nextStmt->GetRootNode();
531-
532562
// Bail if types disagree.
533563
// Might be able to tolerate these by retyping.
534564
//

0 commit comments

Comments
 (0)