boolean hasLoops(CFG cfg, int[]visitStatus, int index) { final int VISITING = 1; final int VISITED = 2; // reached an instruction with no successor (probably a return instruction) if (cfg.succsOf(index) == null) return false; // Check if we've visited the node and finished exploring all sub-branches if (visitStatus[index] == VISITED) return false; // Check if we've looped back to visit a node we haven't finished exploring if (visitStatus[index] == VISITING) return true; // Mark node as being explored visitStatus[index] = VISITING; // Visit successors of this node and see if we loop back for (int succ: cfg.succsOf(index)) if (hasLoops(cfg, visitStatus, succ)) return true; // Mark this node as having been explored successfully visitStatus[index] = VISITED; return false; } }
if (hasLoops(cfg, visitStatus, 0)) return null;