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

Code before:
                    GenTree* jumpTable = switchTree->AsOp()->gtOp2;
                    assert(jumpTable->OperIs(GT_JMPTABLE));
                    blockRange->Remove(jumpTable);
                }

                // The highest case index is caseCount - 2 (caseCount includes the default case).
                // Using an unsigned GT comparison handles negative values correctly because they
                // wrap to large unsigned values, making them greater than maxCaseIndex as expected.
                const unsigned maxCaseIndex = caseCount - 2;

                JITDUMP("\nConverting a switch (" FMT_BB
                        ") where all non-default cases target the same block to a "
                        "conditional branch. Before:\n",
                        block->bbNum);
                DISPNODE(switchTree);

                switchTree->ChangeOper(GT_JTRUE);
                GenTree* maxCaseNode = gtNewIconNode(maxCaseIndex, genActualType(switchVal->TypeGet()));

                // Choose the comparison direction so the fall-through (false edge)
                // targets the lexically next block when possible.
                GenTree*  condNode;
                FlowEdge* trueEdge;
                FlowEdge* falseEdge;

                if (block->NextIs(defaultDest))
                {
                    // defaultDest is next: use GT_LE so false (out of range) falls through
                    // to defaultDest
                    condNode = gtNewOperNode(GT_LE, TYP_INT, switchVal, maxCaseNode);
                    trueEdge = firstCaseEdge;
                    falseEdge =
                        (switchDesc->GetSucc(0) == firstCaseEdge) ? switchDesc->GetSucc(1) : switchDesc->GetSucc(0);
                }
                else
                {
                    // caseDest is next, or neither is next: use GT_GT so false (in range)
                    // falls through to caseDest, which is typically the hotter path
                    condNode = gtNewOperNode(GT_GT, TYP_INT, switchVal, maxCaseNode);
                    trueEdge =
                        (switchDesc->GetSucc(0) == firstCaseEdge) ? switchDesc->GetSucc(1) : switchDesc->GetSucc(0);
                    falseEdge = firstCaseEdge;
                }

                assert(trueEdge != nullptr);
                assert(falseEdge != nullptr);

                condNode->SetUnsigned();
                switchTree->AsOp()->gtOp1 = condNode;
                switchTree->AsOp()->gtOp1->gtFlags |= (GTF_RELOP_JMP_USED | GTF_DONT_CSE);


Review comment (suggestion):
We generally avoid relying on lexical order, you can delete this if and unconditionally use the GT_LE path. We used to do it, but it's no longer required.

Code after:
        }

        block->SetCond(firstCaseEdge, defaultEdge);

        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
//
//  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())