diff --git a/src/coreclr/jit/redundantbranchopts.cpp b/src/coreclr/jit/redundantbranchopts.cpp index 55443f60a15464..9fa10a7b83a139 100644 --- a/src/coreclr/jit/redundantbranchopts.cpp +++ b/src/coreclr/jit/redundantbranchopts.cpp @@ -1961,9 +1961,8 @@ bool Compiler::optRedundantRelop(BasicBlock* const block) continue; } - // If the lcl defined here is live out, forward sub is problematic. - // We'll either create a redundant tree (as the original won't be dead) - // or lose the def (if we actually move the RHS tree). + // If the lcl defined here is live out we'll create a redundant tree + // (as the original won't be dead). // if (VarSetOps::IsMember(this, block->bbLiveOut, prevTreeLclDsc->lvVarIndex)) { @@ -1971,6 +1970,17 @@ bool Compiler::optRedundantRelop(BasicBlock* const block) continue; } + // The local may have uses between the def and the JTRUE. Duplicating + // reads is not allowed and in any case duplicating it in this case is + // probably not be worth it from a profitability perspective. + // + LclSsaVarDsc* ssaDefDsc = prevTreeLclDsc->GetPerSsaData(prevTreeLHS->AsLclVarCommon()->GetSsaNum()); + if (ssaDefDsc->GetNumUses() >= 2) + { + JITDUMP(" -- prev tree lcl V%02u has up to %d uses\n", prevTreeLcl, ssaDefDsc->GetNumUses()); + continue; + } + JITDUMP(" -- prev tree is viable candidate for relop fwd sub!\n"); candidateTree = prevTreeRHS; candidateStmt = prevStmt;