diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index d7ce753002c5a..beed88b04e8b6 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -6357,7 +6357,7 @@ class Compiler bool optIsProfitableToHoistableTree(GenTree* tree, unsigned lnum); // Performs the hoisting 'tree' into the PreHeader for loop 'lnum' - void optHoistCandidate(GenTree* tree, unsigned lnum, LoopHoistContext* hoistCtxt); + void optHoistCandidate(GenTree* tree, BasicBlock* treeBb, unsigned lnum, LoopHoistContext* hoistCtxt); // Returns true iff the ValueNum "vn" represents a value that is loop-invariant in "lnum". // Constants and init values are always loop invariant. @@ -6387,7 +6387,7 @@ class Compiler bool optComputeLoopSideEffectsOfBlock(BasicBlock* blk); // Hoist the expression "expr" out of loop "lnum". - void optPerformHoistExpr(GenTree* expr, unsigned lnum); + void optPerformHoistExpr(GenTree* expr, BasicBlock* exprBb, unsigned lnum); public: void optOptimizeBools(); diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index fd17d74e4d089..d9753972d903d 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -5462,7 +5462,7 @@ int Compiler::optIsSetAssgLoop(unsigned lnum, ALLVARSET_VALARG_TP vars, varRefKi return 0; } -void Compiler::optPerformHoistExpr(GenTree* origExpr, unsigned lnum) +void Compiler::optPerformHoistExpr(GenTree* origExpr, BasicBlock* exprBb, unsigned lnum) { #ifdef DEBUG if (verbose) @@ -5476,6 +5476,8 @@ void Compiler::optPerformHoistExpr(GenTree* origExpr, unsigned lnum) } #endif + assert(exprBb != nullptr); + // This loop has to be in a form that is approved for hoisting. assert(optLoopTable[lnum].lpFlags & LPFLG_HOISTABLE); @@ -5512,6 +5514,8 @@ void Compiler::optPerformHoistExpr(GenTree* origExpr, unsigned lnum) compCurBB = preHead; hoist = fgMorphTree(hoist); + preHead->bbFlags |= (exprBb->bbFlags & (BBF_HAS_IDX_LEN | BBF_HAS_NULLCHECK)); + Statement* hoistStmt = gtNewStmt(hoist); hoistStmt->SetCompilerAdded(); @@ -6205,6 +6209,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack* blo bool m_beforeSideEffect; unsigned m_loopNum; LoopHoistContext* m_hoistContext; + BasicBlock* m_currentBlock; bool IsNodeHoistable(GenTree* node) { @@ -6297,11 +6302,13 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack* blo , m_beforeSideEffect(true) , m_loopNum(loopNum) , m_hoistContext(hoistContext) + , m_currentBlock(nullptr) { } void HoistBlock(BasicBlock* block) { + m_currentBlock = block; for (Statement* const stmt : block->NonPhiStatements()) { WalkTree(stmt->GetRootNodePointer(), nullptr); @@ -6309,7 +6316,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack* blo if (m_valueStack.TopRef().m_hoistable) { - m_compiler->optHoistCandidate(stmt->GetRootNode(), m_loopNum, m_hoistContext); + m_compiler->optHoistCandidate(stmt->GetRootNode(), block, m_loopNum, m_hoistContext); } m_valueStack.Reset(); @@ -6612,7 +6619,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack* blo value.m_hoistable = false; value.m_invariant = false; - m_compiler->optHoistCandidate(value.Node(), m_loopNum, m_hoistContext); + m_compiler->optHoistCandidate(value.Node(), m_currentBlock, m_loopNum, m_hoistContext); } } } @@ -6654,7 +6661,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack* blo } } -void Compiler::optHoistCandidate(GenTree* tree, unsigned lnum, LoopHoistContext* hoistCtxt) +void Compiler::optHoistCandidate(GenTree* tree, BasicBlock* treeBb, unsigned lnum, LoopHoistContext* hoistCtxt) { assert(lnum != BasicBlock::NOT_IN_LOOP); assert((optLoopTable[lnum].lpFlags & LPFLG_HOISTABLE) != 0); @@ -6679,7 +6686,7 @@ void Compiler::optHoistCandidate(GenTree* tree, unsigned lnum, LoopHoistContext* } // Expression can be hoisted - optPerformHoistExpr(tree, lnum); + optPerformHoistExpr(tree, treeBb, lnum); // Increment lpHoistedExprCount or lpHoistedFPExprCount if (!varTypeIsFloating(tree->TypeGet())) diff --git a/src/tests/issues.targets b/src/tests/issues.targets index 32bd76503e4bd..3131652622b30 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -661,9 +661,6 @@ Needs Triage - - https://github.com/dotnet/runtime/issues/57774 -