Repository: dotnet/runtime
File: src/coreclr/jit/fgopt.cpp (C++)

Code before:
        }

        block->SetCond(firstCaseEdge, defaultEdge);

        // The switch-to-cond conversion preserved edge likelihoods but
        // successor block weights may be stale (they were set during import
        // based on the original switch topology). Recompute them so that
        // downstream passes like block compaction see correct weights.
        if (block->hasProfileWeight())
        {
            if (caseDest->hasProfileWeight())
            {
                weight_t oldWeight = caseDest->bbWeight;
                caseDest->setBBProfileWeight(caseDest->computeIncomingWeight());
                JITDUMP("Updated " FMT_BB " (caseDest) profile weight from " FMT_WT " to " FMT_WT "\n", caseDest->bbNum,
                        oldWeight, caseDest->bbWeight);
            }
            if (defaultDest->hasProfileWeight())
            {
                weight_t oldWeight = defaultDest->bbWeight;
                defaultDest->setBBProfileWeight(defaultDest->computeIncomingWeight());
                JITDUMP("Updated " FMT_BB " (defaultDest) profile weight from " FMT_WT " to " FMT_WT "\n",
                        defaultDest->bbNum, oldWeight, defaultDest->bbWeight);
            }
        }

        JITDUMP("After:\n");
        DISPNODE(switchTree);

        if (fgFoldCondToReturnBlock(block))
        {
            JITDUMP("Folded conditional return into branchless return. After:\n");
            DISPNODE(switchTree);
        }

        return true;
    }

    return modified;
}

//-------------------------------------------------------------
// fgBlockEndFavorsTailDuplication:
//     Heuristic function that returns true if this block ends in a statement that looks favorable
//     for tail-duplicating its successor (such as assigning a constant to a local).
//
//  Arguments:
//      block: BasicBlock we are considering duplicating the successor of
//      lclNum: local that is used by the successor block, provided by
//        prior call to fgBlockIsGoodTailDuplicationCandidate
//

Review comment (suggestion):
```suggestion

```

I don't see why we need this. The true and false blocks aren't expected to change their weights in this optimization (and so are edges). I've ran SPMI without this check and it finished without errors.

Code after:
}

//-------------------------------------------------------------
// fgBlockEndFavorsTailDuplication:
//     Heuristic function that returns true if this block ends in a statement that looks favorable
//     for tail-duplicating its successor (such as assigning a constant to a local).
//
//  Arguments:
//      block: BasicBlock we are considering duplicating the successor of
//      lclNum: local that is used by the successor block, provided by
//        prior call to fgBlockIsGoodTailDuplicationCandidate
//
//  Returns:
//     true if block end is favorable for tail duplication
//
//  Notes:
//     This is the second half of the evaluation for tail duplication, where we try
//     to determine if this predecessor block assigns a constant or provides useful
//     information about a local that is tested in an unconditionally executed successor.
//     If so then duplicating the successor will likely allow the test to be
//     optimized away.
//
bool Compiler::fgBlockEndFavorsTailDuplication(BasicBlock* block, unsigned lclNum)
{
    if (block->isRunRarely())
    {
        return false;
    }

    // If the local is address exposed, we currently can't optimize.
    //
    LclVarDsc* const lclDsc = lvaGetDesc(lclNum);

    if (lclDsc->IsAddressExposed())
    {
        return false;
    }

    Statement* const lastStmt  = block->lastStmt();
    Statement* const firstStmt = block->FirstNonPhiDef();

    if (lastStmt == nullptr)
    {
        return false;
    }

    // Tail duplication tends to pay off when the last statement
    // is a local store of a constant, arraylength, or a relop.
    // This is because these statements produce information about values
    // that would otherwise be lost at the upcoming merge point.
    //