Skip to content

7.4 Find dominators from several entries

You get the dominators of a graph with several entries, and the dominator forest they form.

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.

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?

The calls, the entries and the methods asked about
The dominator forest from main and load
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Entry or method asked about
Output
  • 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.

// Besides main, a background job calls load directly.
Dominators dominators =
Dominators.of(BiAdjacentGraph.of(calls), IntList.of(main, load));
// -1 - an entry
int immediateDominatorOfLoad = dominators.immediateDominatorOf(load);
// 3 - load
int 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
Full example
// Besides main, a background job calls load directly.
let dominators = Dominators::of_entries(&BiAdjacentGraph::of(&calls), &[main, load]);
// None - an entry
let immediate_dominator_of_load = dominators.immediate_dominator_of(load);
// Some(3) - load
let immediate_dominator_of_parse = dominators.immediate_dominator_of(parse);
// Some(2) - run
let 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
Full example
// Besides main, a background job calls load directly.
const dominators = Dominators.of(BiAdjacentGraph.of(calls), [main, load]);
// -1 - an entry
const immediateDominatorOfLoad = dominators.immediateDominatorOf(load);
// 3 - load
const immediateDominatorOfParse = dominators.immediateDominatorOf(parse);
// 2 - run
const 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
Full example
// Besides main, a background job calls load directly.
Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(calls), [main, load]);
// -1 - an entry
int immediateDominatorOfLoad = dominators.ImmediateDominatorOf(load);
// 3 - load
int immediateDominatorOfParse = dominators.ImmediateDominatorOf(parse);
// 2 - run
int 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 -> write
Full example

load 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.