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;
}

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 ; }
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 .
