@@ -5280,6 +5280,13 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
52805280    weight_t                minBlockSoFar         = BB_MAX_WEIGHT;
52815281    BasicBlock*            bbHavingAlign         = nullptr ;
52825282    BasicBlock::loopNumber currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;
5283+     bool                    visitedLoopNum[BasicBlock::MAX_LOOP_NUM];
5284+     memset (visitedLoopNum, false , sizeof (visitedLoopNum));
5285+ 
5286+ #ifdef  DEBUG
5287+     unsigned  visitedBlockForLoopNum[BasicBlock::MAX_LOOP_NUM];
5288+     memset (visitedBlockForLoopNum, 0 , sizeof (visitedBlockForLoopNum));
5289+ #endif 
52835290
52845291    if  ((fgFirstBB != nullptr ) && fgFirstBB->isLoopAlign ())
52855292    {
@@ -5302,7 +5309,7 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
53025309            }
53035310        }
53045311
5305-         //  If there is a  unconditional jump (which is not part of callf/always pair)
5312+         //  If there is an  unconditional jump (which is not part of callf/always pair)
53065313        if  (opts.compJitHideAlignBehindJmp  && (block->bbJumpKind  == BBJ_ALWAYS) && !block->isBBCallAlwaysPairTail ())
53075314        {
53085315            //  Track the lower weight blocks
@@ -5356,12 +5363,19 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
53565363                madeChanges       = true ;
53575364                unmarkedLoopAlign = true ;
53585365            }
5359-             else  if  ((block ->bbNatLoopNum  != BasicBlock::NOT_IN_LOOP) && (block-> bbNatLoopNum  ==  loopTop->bbNatLoopNum ) )
5366+             else  if  ((loopTop ->bbNatLoopNum  != BasicBlock::NOT_IN_LOOP) && visitedLoopNum[ loopTop->bbNatLoopNum ] )
53605367            {
5368+ #ifdef  DEBUG
5369+                 char  buffer[100 ];
5370+                 sprintf_s (buffer, 100 , " loop block "   FMT_BB "  appears before top of loop"  ,
5371+                           visitedBlockForLoopNum[loopTop->bbNatLoopNum ]);
5372+ #endif 
5373+ 
53615374                //  In some odd cases we may see blocks within the loop before we see the
53625375                //  top block of the loop. Just bail on aligning such loops.
53635376                // 
5364-                 loopTop->unmarkLoopAlign (this  DEBUG_ARG (" loop block appears before top of loop"  ));
5377+ 
5378+                 loopTop->unmarkLoopAlign (this  DEBUG_ARG (buffer));
53655379                madeChanges       = true ;
53665380                unmarkedLoopAlign = true ;
53675381            }
@@ -5396,6 +5410,20 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
53965410                break ;
53975411            }
53985412        }
5413+ 
5414+         if  (block->bbNatLoopNum  != BasicBlock::NOT_IN_LOOP)
5415+         {
5416+ #ifdef  DEBUG
5417+             if  (!visitedLoopNum[block->bbNatLoopNum ])
5418+             {
5419+                 //  Record the first block for which bbNatLoopNum was seen for
5420+                 //  debugging purpose.
5421+                 visitedBlockForLoopNum[block->bbNatLoopNum ] = block->bbNum ;
5422+             }
5423+ #endif 
5424+             //  If this block is part of loop, mark the loopNum as visited.
5425+             visitedLoopNum[block->bbNatLoopNum ] = true ;
5426+         }
53995427    }
54005428
54015429    assert (loopsToProcess == 0 );
0 commit comments