From b049f42e5977ead2735402a79ad8247441cc3c7c Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Wed, 20 Sep 2023 01:39:30 +0200 Subject: [PATCH] Fix optSwitchConvert (#92249) Co-authored-by: Egor --- src/coreclr/jit/switchrecognition.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/switchrecognition.cpp b/src/coreclr/jit/switchrecognition.cpp index a31de6a97bce7..5052e6ff57411 100644 --- a/src/coreclr/jit/switchrecognition.cpp +++ b/src/coreclr/jit/switchrecognition.cpp @@ -289,8 +289,11 @@ bool Compiler::optSwitchConvert(BasicBlock* firstBlock, int testsCount, ssize_t* maxValue = newMaxValue; } - assert(testIdx <= testsCount); - if (testIdx < SWITCH_MIN_TESTS) + // testIdx is now representing the index of last good test value, + // Update testsCount as it's now potentially smaller than initially. + testsCount = testIdx; + + if (testsCount < SWITCH_MIN_TESTS) { // Make sure we still have at least SWITCH_MIN_TESTS values after we filtered out some of them return false; @@ -304,7 +307,7 @@ bool Compiler::optSwitchConvert(BasicBlock* firstBlock, int testsCount, ssize_t* // Find the last block in the chain const BasicBlock* lastBlock = firstBlock; - for (int i = 0; i < testIdx - 1; i++) + for (int i = 0; i < testsCount - 1; i++) { lastBlock = lastBlock->bbNext; }