hphp/runtime/vm/jit/cfg.cpp
/*
 * Find the immediate dominator of each block using Cooper, Harvey, and
 * Kennedy's "A Simple, Fast Dominance Algorithm", returned as a vector
 * of Block*, indexed by block.  IdomVector[b] == nullptr if b has no
 * dominator.  This is the case for the entry block and any blocks not
 * reachable from the entry block.
 */
Find the immediate dominator of each block using Cooper, Harvey, and
Kennedy's "A Simple, Fast Dominance Algorithm", returned as a vector
of Block*, indexed by block.  IdomVector[b] == nullptr if b has no
dominator.  This is the case for the entry block and any blocks not
reachable from the entry block.
Find the immediate dominator of each block using Cooper Harvey and Kennedy ' s " A Simple Fast Dominance Algorithm " returned as a vector of Block * indexed by block . IdomVector [ b ] == nullptr if b has no dominator . This is the case for the entry block and any blocks not reachable from the entry block .
IdomVector findDominators(const IRUnit& unit, const BlockList& blocks) {
  assert(isRPOSorted(blocks));
  IdomVector idom(unit, nullptr);
  auto start = blocks.begin();
  auto entry = *start;
  idom[entry] = entry;
  start++;
  for (bool changed = true; changed; ) {
    changed = false;
    for (auto it = start; it != blocks.end(); it++) {
      Block* block = *it;
      auto predIter = block->preds().begin();
      auto predEnd = block->preds().end();
      auto p1 = predIter->inst()->block();
      while (!idom[p1]) p1 = (++predIter)->inst()->block();
      for (++predIter; predIter != predEnd; ++predIter) {
        auto p2 = predIter->inst()->block();
        if (p2 == p1 || !idom[p2]) continue;
        do {
          while (p1->postId() < p2->postId()) p1 = idom[p1];
          while (p2->postId() < p1->postId()) p2 = idom[p2];
        } while (p1 != p2);
      }
      if (idom[block] != p1) {
        idom[block] = p1;
        changed = true;
      }
    }
  }
  idom[entry] = nullptr; 
  return idom;
}
IdomVector findDominators ( const IRUnit & unit , const BlockList & blocks ) { assert ( isRPOSorted ( blocks ) ) ; IdomVector idom ( unit , nullptr ) ; auto start = blocks . begin ( ) ; auto entry = * start ; idom [ entry ] = entry ; start ++ ; for ( bool changed = true ; changed ; ) { changed = false ; for ( auto it = start ; it != blocks . end ( ) ; it ++ ) { Block * block = * it ; auto predIter = block -> preds ( ) . begin ( ) ; auto predEnd = block -> preds ( ) . end ( ) ; auto p1 = predIter -> inst ( ) -> block ( ) ; while ( ! idom [ p1 ] ) p1 = ( ++ predIter ) -> inst ( ) -> block ( ) ; for ( ++ predIter ; predIter != predEnd ; ++ predIter ) { auto p2 = predIter -> inst ( ) -> block ( ) ; if ( p2 == p1 || ! idom [ p2 ] ) continue ; do { while ( p1 -> postId ( ) < p2 -> postId ( ) ) p1 = idom [ p1 ] ; while ( p2 -> postId ( ) < p1 -> postId ( ) ) p2 = idom [ p2 ] ; } while ( p1 != p2 ) ; } if ( idom [ block ] != p1 ) { idom [ block ] = p1 ; changed = true ; } } } idom [ entry ] = nullptr ; return idom ; }
Find the immediate dominator of each block using Cooper, Harvey, and
 Kennedy's "A Simple, Fast Dominance Algorithm", returned as a vector
 of Block*, indexed by block.
Find the immediate dominator of each block using Cooper Harvey and Kennedy ' s " A Simple Fast Dominance Algorithm " returned as a vector of Block * indexed by block .
// Calculate immediate dominators with the iterative two-finger algorithm. // When it terminates, idom[post-id] will contain the post-id of the // immediate dominator of each block.  idom[start] will be -1.  This is // the general algorithm but it will only loop twice for loop-free graphs. // for each block after start, in reverse postorder // p1 = any already-processed predecessor // for all other already-processed predecessors p2 of block // find earliest common predecessor of p1 and p2 // (higher postIds are earlier in flow and in dom-tree). // entry has no dominator.