question: EdgeMap < Block > findLoops ( Deque < Block > code , Map < Block , Block > idom , SetMap < Block , Edge > pred ) { EdgeMap < Block > loops = new EdgeMap < Block > ( ) ; for ( Block b : code ) for ( Edge s : b . succ ( ) ) if ( isLoop ( s , idom ) ) { verboseStatus ( "backedge " + s ) ; Block h = s . to ; Set < Block > loop = loops . get ( h ) ; Set < Block > work = new TreeSet < Block > ( ) ; for ( Edge p : pred . get ( h ) ) { if ( isLoop ( p , idom ) && ! loop . contains ( p . from ) && p . from != h ) { loop . add ( p . from ) ; work . add ( p . from ) ; } } while ( ! work . isEmpty ( ) ) { Block x = getBlock ( work ) ; for ( Edge p : pred . get ( x ) ) { if ( p . from != h && ! loop . contains ( p . from ) ) { loop . add ( p . from ) ; work . add ( p . from ) ; } } } } return loops ; }
target:  loop code layout the loop is defined by the back edge from T->H.  H (header) is the source of the back edge.  We want H at the bottom of the loop and T at the top. Lay the blocks out in DFS order starting with T and skipping any blocks that aren't part of the loop. prefer to have fall-through blocks that are part of the loop.