From c95095e5e2ff3ddad20933cffe6b0b1de6374214 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Thu, 18 Jul 2024 20:39:09 +0200 Subject: [PATCH] JIT: Skip strength reductions that are just widenings (#105075) The previous equality check does not catch the case where the SCEV is just a widened version of the previous primary IV. We want to leave that handling up to IV widening. --- src/coreclr/jit/inductionvariableopts.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/inductionvariableopts.cpp b/src/coreclr/jit/inductionvariableopts.cpp index 5f88917d65ec12..9ae98dfbf7770d 100644 --- a/src/coreclr/jit/inductionvariableopts.cpp +++ b/src/coreclr/jit/inductionvariableopts.cpp @@ -1452,10 +1452,25 @@ bool StrengthReductionContext::TryStrengthReduce() DBEXEC(VERBOSE, currentIV->Dump(m_comp)); JITDUMP("\n"); - if (Scev::Equals(currentIV->Step, primaryIV->Step) && !StressProfitability()) + if (!StressProfitability()) { - JITDUMP(" Skipping: candidate has same step as primary IV\n"); - continue; + if (Scev::Equals(currentIV->Step, primaryIV->Step)) + { + JITDUMP(" Skipping: Candidate has same step as primary IV\n"); + continue; + } + + // Leave widening up to widening. + int64_t newIVStep; + int64_t primaryIVStep; + if (currentIV->Step->TypeIs(TYP_LONG) && primaryIV->Step->TypeIs(TYP_INT) && + currentIV->Step->GetConstantValue(m_comp, &newIVStep) && + primaryIV->Step->GetConstantValue(m_comp, &primaryIVStep) && + (int32_t)newIVStep == (int32_t)primaryIVStep) + { + JITDUMP(" Skipping: Candidate has same widened step as primary IV\n"); + continue; + } } if (TryReplaceUsesWithNewPrimaryIV(cursors, currentIV))