Skip to content

Commit

Permalink
Propagate BBF flags during hoisting (#57825)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Aug 26, 2021
1 parent efb54cb commit 6d5a772
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 12 additions & 5 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -6205,6 +6209,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack<BasicBlock*>* blo
bool m_beforeSideEffect;
unsigned m_loopNum;
LoopHoistContext* m_hoistContext;
BasicBlock* m_currentBlock;

bool IsNodeHoistable(GenTree* node)
{
Expand Down Expand Up @@ -6297,19 +6302,21 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack<BasicBlock*>* 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);
assert(m_valueStack.TopRef().Node() == stmt->GetRootNode());

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();
Expand Down Expand Up @@ -6612,7 +6619,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack<BasicBlock*>* 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);
}
}
}
Expand Down Expand Up @@ -6654,7 +6661,7 @@ void Compiler::optHoistLoopBlocks(unsigned loopNum, ArrayStack<BasicBlock*>* 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);
Expand All @@ -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()))
Expand Down
3 changes: 0 additions & 3 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,6 @@
<ExcludeList Include="$(XunitTestBinBase)/JIT/Regression/JitBlue/GitHub_19601/Github_19601/*">
<Issue>Needs Triage</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/JIT/Regression/JitBlue/Runtime_56953/Runtime_56953/*">
<Issue>https://github.com/dotnet/runtime/issues/57774</Issue>
</ExcludeList>
</ItemGroup>

<!-- Unix arm64 specific -->
Expand Down

0 comments on commit 6d5a772

Please sign in to comment.