@@ -956,7 +956,7 @@ LinearScan::LinearScan(Compiler* theCompiler)
956
956
// Notes:
957
957
// On return, the blockSequence array contains the blocks in reverse post-order.
958
958
// This method clears the bbVisitedSet on LinearScan, and when it returns the set
959
- // contains all the bbNums for the block.
959
+ // contains all the bbPostorderNums for the block.
960
960
//
961
961
void LinearScan::setBlockSequence()
962
962
{
@@ -967,30 +967,33 @@ void LinearScan::setBlockSequence()
967
967
bbVisitedSet = BitVecOps::MakeEmpty(traits);
968
968
969
969
assert((blockSequence == nullptr) && (bbSeqCount == 0));
970
-
971
- compiler->m_dfsTree = compiler->fgComputeDfs</* useProfile */ true>();
972
- FlowGraphDfsTree* const dfsTree = compiler->m_dfsTree;
973
- blockSequence = new (compiler, CMK_LSRA) BasicBlock*[compiler->fgBBcount];
970
+ blockSequence = new (compiler, CMK_LSRA) BasicBlock*[compiler->fgBBcount];
974
971
975
972
if (compiler->opts.OptimizationEnabled())
976
973
{
977
- // Ensure loop bodies are compact in the visitation order.
978
- compiler->m_loops = FlowGraphNaturalLoops::Find(dfsTree);
974
+ // If optimizations are enabled, allocate blocks in reverse post-order.
975
+ // This ensures each block's predecessors are visited first.
976
+ // Also, ensure loop bodies are compact in the visitation order.
977
+ compiler->m_dfsTree = compiler->fgComputeDfs</* useProfile */ true>();
978
+ compiler->m_loops = FlowGraphNaturalLoops::Find(compiler->m_dfsTree);
979
979
FlowGraphNaturalLoops* const loops = compiler->m_loops;
980
- unsigned index = 0;
981
980
982
- auto addToSequence = [this, &index ](BasicBlock* block) {
983
- blockSequence[index ++] = block;
981
+ auto addToSequence = [this](BasicBlock* block) {
982
+ blockSequence[bbSeqCount ++] = block;
984
983
};
985
984
986
- compiler->fgVisitBlocksInLoopAwareRPO(dfsTree , loops, addToSequence);
985
+ compiler->fgVisitBlocksInLoopAwareRPO(compiler->m_dfsTree , loops, addToSequence);
987
986
}
988
987
else
989
988
{
990
- // TODO: Just use lexical block order in MinOpts
991
- for (unsigned i = 0; i < dfsTree->GetPostOrderCount(); i++)
989
+ // If we aren't optimizing, we won't have any cross-block live registers,
990
+ // so the order of blocks allocated shouldn't matter.
991
+ // Just use the linear order.
992
+ for (BasicBlock* const block : compiler->Blocks())
992
993
{
993
- blockSequence[i] = dfsTree->GetPostOrder(dfsTree->GetPostOrderCount() - i - 1);
994
+ // Give this block a unique post-order number that can be used as a key into bbVisitedSet
995
+ block->bbPostorderNum = bbSeqCount;
996
+ blockSequence[bbSeqCount++] = block;
994
997
}
995
998
}
996
999
@@ -1094,30 +1097,29 @@ void LinearScan::setBlockSequence()
1094
1097
};
1095
1098
1096
1099
JITDUMP("Start LSRA Block Sequence: \n");
1097
- for (unsigned i = 0; i < dfsTree->GetPostOrderCount() ; i++)
1100
+ for (unsigned i = 0; i < bbSeqCount ; i++)
1098
1101
{
1099
1102
visitBlock(blockSequence[i]);
1100
1103
}
1101
1104
1102
- // If the DFS didn't visit any blocks, add them to the end of blockSequence
1103
- if (dfsTree->GetPostOrderCount() < compiler->fgBBcount)
1105
+ // If any blocks remain unvisited, add them to the end of blockSequence.
1106
+ // Unvisited blocks are more likely to be at the back of the list, so iterate backwards.
1107
+ for (BasicBlock* block = compiler->fgLastBB; bbSeqCount < compiler->fgBBcount; block = block->Prev())
1104
1108
{
1105
- // Unvisited blocks are more likely to be at the back of the list, so iterate backwards
1106
- unsigned i = dfsTree->GetPostOrderCount();
1107
- for (BasicBlock* block = compiler->fgLastBB; i < compiler->fgBBcount; block = block->Prev())
1109
+ assert(compiler->opts.OptimizationEnabled());
1110
+ assert(block != nullptr);
1111
+ assert(compiler->m_dfsTree != nullptr);
1112
+
1113
+ if (!compiler->m_dfsTree->Contains(block))
1108
1114
{
1109
- assert(block != nullptr);
1110
- if (!dfsTree->Contains(block))
1111
- {
1112
- // Give this block a unique post-order number that can be used as a key into bbVisitedSet
1113
- block->bbPostorderNum = i;
1114
- visitBlock(block);
1115
- blockSequence[i++] = block;
1116
- }
1115
+ // Give this block a unique post-order number that can be used as a key into bbVisitedSet
1116
+ block->bbPostorderNum = bbSeqCount;
1117
+ visitBlock(block);
1118
+ blockSequence[bbSeqCount++] = block;
1117
1119
}
1118
1120
}
1119
1121
1120
- bbSeqCount = compiler->fgBBcount;
1122
+ assert( bbSeqCount == compiler->fgBBcount) ;
1121
1123
blockSequencingDone = true;
1122
1124
1123
1125
#ifdef DEBUG
0 commit comments