Title: Control Flow Analysis
1Control Flow Analysis
- Loops
- Reducibility
- Ordering Nodes of CFG
- Control Dependence
2Loops
- A subflowgraph G ( N, E,s) is a loop with
entry point s iff - for each (n,m) in E and m in N, then either n
in N or m s - for each pair of nodes n, m in N, there are
non-trivial paths from n to m and from m to n -
- The first condition ensures a single entry point.
The second condition ensures the graph is
strongly connected and non-trivial.
3Loop Entry Points
We can use the dominance relationship to identify
all the loops in a procedure
- A backward edge is one whose direction is
inverse to the direction of dominance - So it goes from a node to a dominator of that
node - A loop containing a backward edge to the node
that is the entry point. - In fact, this characterizes loop entry points
A node s in N is the entry point of a loop in G
iff there is a node n in N such that (n,s)
in E and s lt n (backward
edge)
4Loop Entry Points
- Once we have the entry point, we can identify the
nodes in the loop - The maximum loop with entry point s is
- the subflowgraph G with initial node s that is
generated by the set N - N m in N there is a path from m to s
that only contains nodes dominated by s
5Algorithm to Find Loops
- First step
- compute the dominance relation for flowgraph.
- Second step
- find set of nodes s in flowgraph for which there
is a node n such that - s lt n in dominance relationship and
- (n,s) is an edge in flowgraph.
Each such node s is a loop entry point and (n,s)
is a backward edge
6Algorithm to Find Loops
- Third (construction) step
- construct natural loop associated with s and
(n,s). It is characterized by its nodes N N - Enter entry point s and node n at the other end
of the backward edge into N. - Then include the predecessors of n.
- Continue by adding all the predecessors of nodes
that are in the loop, except for the predecessors
of the entry point, until there are no more such
nodes.
7Loop Example
B1
1. StmtA 2. I 1 3. if ( I gt N) goto 7 4.
StmtB 5. I I 1 6. goto 3 7. StmtC
B1
B1 B2 B3 B4
B2
B2
B3
Graph has a backward edge (B3,B2) B2 is entry
point
Dominator tree
Flowgraph
B4
8Limitations on Data Flow Analysis
- Many data flow methods work only with reducible
graphs - Informally, a reducible graph is one where we can
partition the edges into a set of forward edges
and a set of backward edges.
b doesnt dominate c, c doesnt dominate b
An Irreducible Graph
9Reducibility
- In practice we can overcome this problem
- replace an irreducible graph by another graph in
which some nodes have been duplicated - node-splitting
a
c
b
c
10Reducibility
- b 6
- if ( ) goto prev
- else goto next
- prev if ( a 0 ) goto next
- my .. a if ( ) return
- goto prev
- next if ( a 1 ) goto prev
- my a if ( ) return
- goto next
11Reducibility
Node-splitting can help
- b 6
- if ( ) goto prev
- else goto next
- prev if ( a 0 ) goto othernext
- my .. a if ( ) return
goto prev - next if ( a 1 ) goto prev
- my a if ( ) return goto
next - othernext if ( a 1 ) goto prev
- my a if ( ) return goto
othernext
12Traversing Flowgraphs
- In order to deal with data flow problems we must
generally process the nodes of a flowgraph,
sometimes repeatedly. - Sometimes the order in which we process nodes is
important for correctness. - Mostly it is important for efficiency
- the order in which nodes are visited may strongly
affect the execution efficiency. - We give an example of one useful order,
rPostorder. - It is not unique there is more than one
rPostorder for a given flowgraph.
13Depth-first Spanning Tree
- To construct rPostorder for a graph, we first
compute a depth-first spanning tree for it. - Depth-first spanning tree
- a partial subgraph of G containing all nodes of G
that is also a tree - obtained by eliminating edges of G
- Use this to visit descendants of a node before
nodes that are not descendants (bottom-up) - Or to visit nodes before any of their descendants
(top-down)
- Eliminate the edge from 4 to 2 to obtain a tree.
14Depth-first Spanning Tree
We select a subset of edges of G (N, E, s) to
form DFST
Initialization For each node m in N, set mark(m)
to false. Initialize E, set of edges in tree,
to empty set set i to N.
- Begin with s, the root mark it.
- Search for unmarked successors.
- If there are unmarked successors, then no edge in
tree reaches these nodes. - Select one and add the edge from the current node
to this successor to the tree. Mark the selected
successor. - This is a depth-first search, so now apply the
search procedure to the selected node. - When there are no unmarked successors, number the
current node with value of i and decrement i.
151
1
start node
2
2
3
3
5
4
4
5
7
6
7
6
blue ? marked
161
1
edge added
2
2
choose one of successors
3
3
5
4
4
5
7
6
7
6
171
1
visited
2
2
3
3
5
4
4
5
7
6
7
6
181
1
now choose a successor
2
2
3
3
5
4
4
5
7
6
7
6
191
1
depth first, so go here next
2
2
3
3
5
4
4
5
7
6
7
6
201
1
no successor. number node
2
2
3
3
5
4
4
5
7
7
7
6
211
1
no successor. number node
2
2
3
3
5
6
4
5
7
7
7
6
221
1
Visit successor. Number it.
2
2
3
3
5
6
4
5
7
7
7
6
231
1
Number this one.
2
2
4
3
5
6
4
5
7
7
7
6
241
1
Now visit successor of 2, number it.
2
2
4
3
5
6
4
5
3
7
7
6
251
1
2
2
4
3
5
6
4
5
3
7
7
6
26Node Orderings
- Different analyses require us to visit nodes in
different orders - Sometimes we need to visit nodes before any of
the successors are visited - pre-order
- Sometimes we visit all immediate successors of a
node before visiting any other successors - breadth-first search
27Control Dependence
- If the execution of statement S1 determines
whether or not statement S2 will be executed, - then S2 is control dependent on S1.
- Example-
- if ( a b ) then c 5
- execution of the assignment c 5 depends on the
outcome of the conditional, i.e. upon the result
of evaluating ab
28Control Dependence
- Control dependencies must be respected whenever
we reorder executions, so optimizations must take
them into account. - In order to define and compute control
dependencies, we must first create a single exit
flowgraph. - This has a unique end node, or sink.
- It enables us to specify a relationship between
nodes and their predecessors. - If necessary, we artificially create a unique
exit node.
29Single-Exit Flowgraph
- Definition-
- A single-exit flowgraph is a quadruple G
(N,E,s,t) where (N,E,s) is a flowgraph, t in N is
the exit node of G, and there is a path from
every node of G to t. - Note that the inverse flowgraph can be created by
reversing the order of each edge.
30Post-Dominators
- Let G ( N, E, s,t ) be a single-exit
flowgraph, and let n, m be nodes. - Definition n post-dominates m iff every path
from m to t contains n - Definition n directly post-dominates m iff n
post-dominates m and there is no node n such
that n post-dominates n and n post-dominates m. -
- n post-dominates m. Note that n does not post
dominate itself.
31Post-Dominators
- Let G ( N, E, s,t ) be a single-exit
flowgraph, and let n, p be nodes. - Definition p is control dependent on n iff
- there is a non-trivial path from n to p such
that every node n in the path (where n ! n,p)
is post-dominated by p, and - n is not post-dominated by p
p
- Condition 2 ensures that n has two successor
nodes, and (at least) two distinct paths to the
exit node, such that p is not in one of them.
321
2
There is a non-trivial path from 5 to 6. But 6
does not post-dominate 5. So 6 is
control-dependent on 5.
3
4
5
6
7
8
a
9
33Post-Dominance
1
a
2
2
3
1
9
4
4
3
5
8
6
7
6
7
5
8
There is a non-trivial path from 5 to 6. But 6
does not post-dominate 5.
a
9
34Control Dependence
- In order to find loops in a flowgraph, we compute
the dominance tree. - In order to compute control dependencies, we
construct the post-dominator tree. - It is relatively easy to impose an order on
nodes so that we do not visit them randomly