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

Code before:
            for (unsigned i = 1; i < caseCount - 1; i++)
            {
                if (switchDesc->GetCase(i)->getDestinationBlock() != caseDest)
                {
                    allCasesSameTarget = false;
                    break;
                }
            }

            if (allCasesSameTarget)
            {
                GenTree* switchVal = switchTree->AsOp()->gtOp1;
                noway_assert(genActualTypeIsIntOrI(switchVal->TypeGet()));

                // If we are in LIR, remove the jump table from the block.
                if (block->IsLIR())
                {
                    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);
                }

Review comment (suggestion):
I think you can avoid this math and the explanation by using `maxCaseIndex = firstCaseEdge->getDupCount()` and then GT_LT instead of GT_LE

Code after:
        const unsigned caseCount = firstCaseEdge->getDupCount();
        GenTree*       iconNode  = gtNewIconNode(caseCount, genActualType(switchVal->TypeGet()));
        GenTree*       condNode  = gtNewOperNode(GT_LT, TYP_INT, switchVal, iconNode);
        condNode->SetUnsigned();
        switchTree->AsOp()->gtOp1 = condNode;
        switchTree->AsOp()->gtOp1->gtFlags |= (GTF_RELOP_JMP_USED | GTF_DONT_CSE);

        gtSetStmtInfo(switchStmt);
        fgSetStmtSeq(switchStmt);

        // Fix up dup counts: multiple switch cases originally pointed to the same
        // successor, but the conditional branch has exactly one edge per target.
        const unsigned caseDupCount = firstCaseEdge->getDupCount();
        if (caseDupCount > 1)
        {
            firstCaseEdge->decrementDupCount(caseDupCount - 1);
            caseDest->bbRefs -= (caseDupCount - 1);
        }

        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