public LoopFinder(DirectedGraph <T> graph, T entry, DominatorGraph <T> doms) { this.graph = graph; this.entry = entry; this.loopNodes = new HashSet <T>(); this.nodeColor = new Dictionary <T, NodeColor>(); Debug.Assert(entry != null); // Find all nodes that can be reached from the back-edge // predecessors by reversed edges and paint them gray. foreach (var p in graph.Predecessors(entry)) { if (doms.DominatesStrictly(entry, p)) { // Back edge! if (!nodeColor.ContainsKey(p)) { // Unvisited back edge! BackwardVisit(p); } } else if (p == entry) { // Self-loop. loopNodes.Add(p); } } // Find all gray nodes that can be visited from the suspected // loop entry and color them black. Black nodes belong // to the loop. NodeColor color; if (nodeColor.TryGetValue(entry, out color) && color == NodeColor.Gray) { ForwardVisit(entry); } }
private bool IsBackEdge(Region a, Region b) { return doms.DominatesStrictly(b, a); }