@@ -4687,27 +4687,35 @@ void Compiler::fgDoReversePostOrderLayout()
4687
4687
#endif // DEBUG
4688
4688
4689
4689
// Compute DFS of all blocks in the method, using profile data to determine the order successors are visited in.
4690
- // Then, identify any loops in the DFS tree so we can keep their bodies compact.
4691
4690
//
4692
- FlowGraphDfsTree* const dfsTree = fgComputeDfs</* useProfile */ true >();
4693
- FlowGraphNaturalLoops* const loops = FlowGraphNaturalLoops::Find (dfsTree);
4694
- BasicBlock** const rpoSequence = new (this , CMK_BasicBlock) BasicBlock*[dfsTree->GetPostOrderCount ()];
4695
- unsigned index = dfsTree->GetPostOrderCount ();
4696
- auto addToSequence = [rpoSequence, &index ](BasicBlock* block) {
4697
- assert (index != 0 );
4698
- rpoSequence[--index ] = block;
4699
- };
4691
+ FlowGraphDfsTree* const dfsTree = fgComputeDfs</* useProfile */ true >();
4692
+
4693
+ // If LSRA didn't create any new blocks, we can reuse its loop-aware RPO traversal,
4694
+ // which is cached in Compiler::fgBBs.
4695
+ // If the cache isn't available, we need to recompute the loop-aware RPO.
4696
+ //
4697
+ BasicBlock** rpoSequence = fgBBs;
4700
4698
4701
- fgVisitBlocksInLoopAwareRPO (dfsTree, loops, addToSequence);
4699
+ if (rpoSequence == nullptr )
4700
+ {
4701
+ rpoSequence = new (this , CMK_BasicBlock) BasicBlock*[dfsTree->GetPostOrderCount ()];
4702
+ FlowGraphNaturalLoops* const loops = FlowGraphNaturalLoops::Find (dfsTree);
4703
+ unsigned index = 0 ;
4704
+ auto addToSequence = [rpoSequence, &index ](BasicBlock* block) {
4705
+ rpoSequence[index ++] = block;
4706
+ };
4707
+
4708
+ fgVisitBlocksInLoopAwareRPO (dfsTree, loops, addToSequence);
4709
+ }
4702
4710
4703
4711
// Fast path: We don't have any EH regions, so just reorder the blocks
4704
4712
//
4705
4713
if (compHndBBtabCount == 0 )
4706
4714
{
4707
- for (unsigned i = dfsTree->GetPostOrderCount () - 1 ; i != 0 ; i-- )
4715
+ for (unsigned i = 1 ; i < dfsTree->GetPostOrderCount (); i++ )
4708
4716
{
4709
- BasicBlock* const block = rpoSequence[i];
4710
- BasicBlock* const blockToMove = rpoSequence[i - 1 ];
4717
+ BasicBlock* const block = rpoSequence[i - 1 ];
4718
+ BasicBlock* const blockToMove = rpoSequence[i];
4711
4719
4712
4720
if (!block->NextIs (blockToMove))
4713
4721
{
@@ -4756,10 +4764,10 @@ void Compiler::fgDoReversePostOrderLayout()
4756
4764
4757
4765
// Reorder blocks
4758
4766
//
4759
- for (unsigned i = dfsTree->GetPostOrderCount () - 1 ; i != 0 ; i-- )
4767
+ for (unsigned i = 1 ; i < dfsTree->GetPostOrderCount (); i++ )
4760
4768
{
4761
- BasicBlock* const block = rpoSequence[i];
4762
- BasicBlock* const blockToMove = rpoSequence[i - 1 ];
4769
+ BasicBlock* const block = rpoSequence[i - 1 ];
4770
+ BasicBlock* const blockToMove = rpoSequence[i];
4763
4771
4764
4772
// Only reorder blocks within the same EH region -- we don't want to make them non-contiguous
4765
4773
//
0 commit comments