public void forEachBlockDepthFirst(boolean reverse, SsaBasicBlock.Visitor v) {
    BitSet visited = new BitSet(blocks.size());

    // We push the parent first, then the child on the stack.
    Stack<SsaBasicBlock> stack = new Stack<SsaBasicBlock>();

    SsaBasicBlock rootBlock = reverse ? getExitBlock() : getEntryBlock();

    if (rootBlock == null) {
      // in the case there's no exit block
      return;
    }

    stack.add(null); // Start with null parent.
    stack.add(rootBlock);

    while (stack.size() > 0) {
      SsaBasicBlock cur = stack.pop();
      SsaBasicBlock parent = stack.pop();

      if (!visited.get(cur.getIndex())) {
        BitSet children = reverse ? cur.getPredecessors() : cur.getSuccessors();
        for (int i = children.nextSetBit(0); i >= 0; i = children.nextSetBit(i + 1)) {
          stack.add(cur);
          stack.add(blocks.get(i));
        }
        visited.set(cur.getIndex());
        v.visitBlock(cur, parent);
      }
    }
  }
Walks the basic block tree in depth-first order, calling the visitor method once for every
block. This depth-first walk may be run forward from the method entry point or backwards from
the method exit points.

@param reverse true if this should walk backwards from the exit points
@param v {@code non-null;} callback interface. {@code parent} is set unless this is the root
    node
android/android_framework/dex-maker/src/main/java/com/android/dx/ssa/SsaMethod.java