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

Code before:

    ArrayStack<BasicBlock*> stack(getAllocator(CMK_Reachability));
    stack.Push(fromBlock);

    while (!stack.Empty())
    {
        BasicBlock* const nextBlock = stack.Pop();
        assert(nextBlock != toBlock);

        if (nextBlock == excludedBlock)
        {
            continue;
        }
        BasicBlockVisit result;
        bool            budgetExceeded = false;
        if (pBudget == nullptr)
        {
            result = nextBlock->VisitAllSuccs(this, [this, toBlock, &stack](BasicBlock* succ) {
                if (succ == toBlock)
                {
                    return BasicBlockVisit::Abort;
                }

                if (BitVecOps::TryAddElemD(optReachableBitVecTraits, optReachableBitVec, succ->bbNum))
                {
                    stack.Push(succ);
                }
                return BasicBlockVisit::Continue;
            });
        }
        else
        {
            result =
                nextBlock->VisitAllSuccs(this, [this, toBlock, &stack, &budgetExceeded, pBudget](BasicBlock* succ) {
                if (succ == toBlock)
                {
                    return BasicBlockVisit::Abort;
                }

                if (--(*pBudget) <= 0)
                {
                    budgetExceeded = true;
                    return BasicBlockVisit::Abort;
                }

                if (BitVecOps::TryAddElemD(optReachableBitVecTraits, optReachableBitVec, succ->bbNum))
                {
                    stack.Push(succ);
                }

                return BasicBlockVisit::Continue;

Review comment (bug):
I think I duplicated the if-else due to a TP regression. In your version you have an extra `(pBudget != nullptr)` check in hot `VisitAllSuccs`, let's see if TP diffs complain. If there is none/it's small then great

Code after:
    {
        assert(BitVecTraits::GetSize(optReachableBitVecTraits) == fgBBNumMax + 1);
        BitVecOps::ClearD(optReachableBitVecTraits, optReachableBitVec);
    }

    ArrayStack<BasicBlock*> stack(getAllocator(CMK_Reachability));
    stack.Push(fromBlock);

    while (!stack.Empty())
    {
        BasicBlock* const nextBlock = stack.Pop();
        assert(nextBlock != toBlock);

        if (nextBlock == excludedBlock)
        {
            continue;
        }
        bool            budgetExceeded = false;
        BasicBlockVisit result =
            nextBlock->VisitAllSuccs(this, [this, toBlock, &stack, &budgetExceeded, pBudget](BasicBlock* succ) {
            if (succ == toBlock)
            {
                return BasicBlockVisit::Abort;
            }

            if ((pBudget != nullptr) && (--(*pBudget) <= 0))
            {
                budgetExceeded = true;
                return BasicBlockVisit::Abort;
            }

            if (BitVecOps::TryAddElemD(optReachableBitVecTraits, optReachableBitVec, succ->bbNum))
            {
                stack.Push(succ);
            }

            return BasicBlockVisit::Continue;
        });

        if (result == BasicBlockVisit::Abort)
        {
            return budgetExceeded ? ReachabilityResult::BudgetExceeded : ReachabilityResult::Reachable;
        }
    }

    return ReachabilityResult::Unreachable;
}