@@ -965,8 +965,10 @@ bool Compiler::fgCanCompactBlocks(BasicBlock* block, BasicBlock* bNext)
965
965
// Arguments:
966
966
// block - move all code into this block.
967
967
// bNext - bbNext of `block`. This block will be removed.
968
+ // doDebugCheck - in Debug builds, check flowgraph for correctness after compaction
969
+ // (some callers might compact blocks during destructive flowgraph changes, and thus should skip checks)
968
970
//
969
- void Compiler::fgCompactBlocks (BasicBlock* block, BasicBlock* bNext)
971
+ void Compiler::fgCompactBlocks (BasicBlock* block, BasicBlock* bNext DEBUGARG ( bool doDebugCheck /* = true */ ) )
970
972
{
971
973
noway_assert (block != nullptr );
972
974
noway_assert (bNext != nullptr );
@@ -1331,7 +1333,7 @@ void Compiler::fgCompactBlocks(BasicBlock* block, BasicBlock* bNext)
1331
1333
#endif
1332
1334
1333
1335
#if DEBUG
1334
- if (JitConfig.JitSlowDebugChecksEnabled () != 0 )
1336
+ if (doDebugCheck && ( JitConfig.JitSlowDebugChecksEnabled () != 0 ) )
1335
1337
{
1336
1338
// Make sure that the predecessor lists are accurate
1337
1339
fgDebugCheckBBlist ();
@@ -4555,6 +4557,23 @@ void Compiler::fgMoveBackwardJumpsToSuccessors()
4555
4557
}
4556
4558
#endif // DEBUG
4557
4559
4560
+ // Compact blocks before trying to move any jumps.
4561
+ // This can unlock more opportunities for fallthrough behavior.
4562
+ //
4563
+ for (BasicBlock* block = fgFirstBB; block != fgFirstFuncletBB;)
4564
+ {
4565
+ if (fgCanCompactBlocks (block, block->Next ()))
4566
+ {
4567
+ // We haven't fixed EH information yet, so don't do any correctness checks here
4568
+ //
4569
+ fgCompactBlocks (block, block->Next () DEBUGARG (/* doDebugCheck */ false ));
4570
+ }
4571
+ else
4572
+ {
4573
+ block = block->Next ();
4574
+ }
4575
+ }
4576
+
4558
4577
EnsureBasicBlockEpoch ();
4559
4578
BlockSet visitedBlocks (BlockSetOps::MakeEmpty (this ));
4560
4579
BlockSetOps::AddElemD (this , visitedBlocks, fgFirstBB->bbNum );
@@ -4747,8 +4766,6 @@ void Compiler::fgDoReversePostOrderLayout()
4747
4766
}
4748
4767
}
4749
4768
4750
- fgMoveBackwardJumpsToSuccessors</* hasEH */ true >();
4751
-
4752
4769
// Fix up call-finally pairs
4753
4770
//
4754
4771
for (int i = 0 ; i < callFinallyPairs.Height (); i++)
@@ -4758,6 +4775,8 @@ void Compiler::fgDoReversePostOrderLayout()
4758
4775
fgInsertBBafter (pair.callFinally , pair.callFinallyRet );
4759
4776
}
4760
4777
4778
+ fgMoveBackwardJumpsToSuccessors</* hasEH */ true >();
4779
+
4761
4780
// The RPO won't change the entry blocks of any EH regions, but reordering can change the last block in a region
4762
4781
// (for example, by pushing throw blocks unreachable via normal flow to the end of the region).
4763
4782
// First, determine the new EH region ends.
0 commit comments