7.4 Find dominators from several entries
You get the dominators of a graph with several entries, and the dominator forest they form.
Problem
Section titled “Problem”You want to know what is certain to have happened before a node, but the paths start at several
nodes. The nodes where the paths of interest start are the entries of the graph. With several
entries, a node d dominates a node n only if every path from any of the entries to n passes
through d. You need this for:
- a program with several entry points, such as a main method and background jobs;
- a method with exception handlers, if the graph has no edges that lead to them;
- a network with several gateways;
- the post-dominators of a method that returns in several places, where the transposed graph has an entry for each exit. See Find the nodes that every path to the exit passes through.
Example
Section titled “Example”The example graph calls models the calls between the methods of a small program. It has an edge
from each method to each method it calls. Besides main, a background job calls load directly, so
the calls have two entries: main and load. Which methods still dominate load, parse and
format, and what is the dominator forest?
- Entry or method asked about
- Method with its dominators as ancestors
- From immediate dominator to method
The picture after shows the dominator forest that the code returns: a tree from main and a tree
from load, with an edge from each method to the methods it immediately dominates.
Solution
Section titled “Solution”// Besides main, a background job calls load directly.Dominators dominators = Dominators.of(BiAdjacentGraph.of(calls), IntList.of(main, load));// -1 - an entryint immediateDominatorOfLoad = dominators.immediateDominatorOf(load);// 3 - loadint immediateDominatorOfParse = dominators.immediateDominatorOf(parse);int immediateDominatorOfFormat = dominators.immediateDominatorOf(format); // 2 - run
// The dominator forest: here a tree from main and one from load.Graph forest = Graphs.dominatorTree(dominators);// forest: main -> parseArgs, main -> run, run -> save, run -> show, run -> format,// load -> parse, load -> read, save -> write// Besides main, a background job calls load directly.let dominators = Dominators::of_entries(&BiAdjacentGraph::of(&calls), &[main, load]);// None - an entrylet immediate_dominator_of_load = dominators.immediate_dominator_of(load);// Some(3) - loadlet immediate_dominator_of_parse = dominators.immediate_dominator_of(parse);// Some(2) - runlet immediate_dominator_of_format = dominators.immediate_dominator_of(format);
// The dominator forest: here a tree from main and one from load.let forest = graphs::dominator_tree(&dominators);// forest: main -> parseArgs, main -> run, run -> save, run -> show, run -> format,// load -> parse, load -> read, save -> write// Besides main, a background job calls load directly.const dominators = Dominators.of(BiAdjacentGraph.of(calls), [main, load]);// -1 - an entryconst immediateDominatorOfLoad = dominators.immediateDominatorOf(load);// 3 - loadconst immediateDominatorOfParse = dominators.immediateDominatorOf(parse);// 2 - runconst immediateDominatorOfFormat = dominators.immediateDominatorOf(format);
// The dominator forest: here a tree from main and one from load.const forest = Graphs.dominatorTree(dominators);// forest: main -> parseArgs, main -> run, run -> save, run -> show, run -> format,// load -> parse, load -> read, save -> write// Besides main, a background job calls load directly.Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(calls), [main, load]);// -1 - an entryint immediateDominatorOfLoad = dominators.ImmediateDominatorOf(load);// 3 - loadint immediateDominatorOfParse = dominators.ImmediateDominatorOf(parse);// 2 - runint immediateDominatorOfFormat = dominators.ImmediateDominatorOf(format);
// The dominator forest: here a tree from main and one from load.Graph forest = Graphs.DominatorTree(dominators);// forest: main -> parseArgs, main -> run, run -> save, run -> show,// run -> format, load -> parse, load -> read, save -> writeload is an entry now and the root of a tree of its own. It has no immediate dominator, and it is
the immediate dominator of parse and read. run still dominates format: every path from
either entry to format passes through run.
Good to know
Section titled “Good to know”- The order of the entries does not matter. An entry given twice counts once, and with no entries, no node is reachable.
- The roots of the forest are of three kinds: the entries, the other nodes whose only strict
dominator is the virtual root, and the nodes that no entry reaches. If
mainalso calledreaddirectly,readwould be of the second kind: the pathmain -> readavoidsload, and the pathload -> readavoidsmain. For all three kinds,Dominators.immediateDominatorOfDominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfreturns-1None-1-1.Dominators.isReachableDominators::is_reachableDominators.isReachableDominators.IsReachabletells the unreachable ones apart.
API used
Section titled “API used”Dominators.of(BiAdjacentGraph, IntList)Dominators::of_entriesDominators.of(graph: BiAdjacentGraph, entries: IntList)Dominators.Of(BiAdjacentGraph, ReadOnlySpan<int>)Dominators.immediateDominatorOf(int)Dominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfGraphs.dominatorTree(Dominators)graphs::dominator_treeGraphs.dominatorTreeGraphs.DominatorTreeBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfDominators.isReachable(int)Dominators::is_reachableDominators.isReachableDominators.IsReachable