7 Dominance
This chapter finds the nodes that every path from an entry passes through, the tree they form, the same towards an exit, and dominators from several entries.
Some nodes cannot be avoided: every path from the entry to a node passes through them. Compilers build on this to find loops and to convert code to SSA form (static single assignment). Analyzers use it to find the checks that guard a piece of code. In a network, it finds the checkpoints that all traffic to a host passes.
Problems in this chapter
Section titled “Problems in this chapter”- 7.1 Find the nodes that every path from the entry passes throughYou get the immediate dominator of each node: the nearest other node that every path from the entry to it passes through.
- 7.2 Build the dominator tree and ask it questionsYou build the tree of immediate dominators as a graph, and answer questions about dominance with an ancestor index.
- 7.3 Find the nodes that every path to the exit passes throughYou find what is certain to happen after a node: its post-dominators, and the post-dominator tree they form.
- 7.4 Find dominators from several entriesYou get the dominators of a graph with several entries, and the dominator forest they form.
The examples of this chapter use the control flow of the method
loadloadloadLoad of a small program, and the calls between its
methods. The picture on this page is a small graph of numbers.
Dominators
Section titled “Dominators”The block where a function starts is the entry of its
control-flow graph. In other graphs, the entry is the node where
the paths of interest start, such as main in the calls of a program. A node d
dominates a node n if every path from the entry to n passes through
d. Every node dominates itself. A node d strictly dominates n if it dominates n and is not
n. The nearest strict dominator of n is called its immediate dominator.
- Dominator of 3
Here, with the entry 0, the nodes that dominate 3 are green. Every path from 0 to 3 passes through 0
and 3. Not every path passes through 1 or through 2, since 0 -> 2 -> 3 avoids 1 and 0 -> 1 -> 3
avoids 2. So 0 is the immediate dominator of 3, and 3 is the immediate dominator of 4.
The dominator tree
Section titled “The dominator tree”The immediate dominators form a tree called the dominator tree. It has an edge from each node to the nodes it immediately dominates. Its root is the entry. A node that the entry does not reach dominates only itself and is the root of a tree of its own, so the dominator tree is then a forest. The dominators of a node are its ancestors in this tree, so every question about dominance is a question about ancestors. See the chapter Trees and hierarchies for such questions. Here, 0 is the parent of 1, 2 and 3 in the dominator tree, and 3 is the parent of 4.
Post-dominators
Section titled “Post-dominators”You can ask the same question in the other direction. A node p post-dominates a node n if every
path from n to an exit passes through p. Turning every edge around gives the
transposed graph. The post-dominators are the dominators of that
graph, with the exits as its entries. Here, with the exit 4, the node 3 post-dominates 0, 1 and 2:
whichever of 1 and 2 follows 0, 3 follows as well.
Several entries
Section titled “Several entries”A graph can have several entries, such as a program with several entry points, or the transposed
graph of a function that returns in several places. A node d then dominates a node n only if
every path from any entry to n passes through d. The entries act as if one extra node had an
edge to each of them. That node is called a virtual root. So an entry has no immediate dominator,
and the dominator tree becomes a forest.
All these analyses follow the edges against their direction as well, so they take a
BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph, which adds the in-nodes of every
node to a graph.