Title: Constructing%20the%20Natural%20Loop%20of%20a%20Back%20Edge
1Constructing the Natural Loop of a Back Edge
- Input a flow graph G and a back edge ngd.
- Output the set loop consisting of all nodes in
the natural loop ngd. - procedure insert(m)
- if m is not in loop then -- check if
visited - loop loop U m -- mark as visited
- push m onto stack -- push for
predecessor search - / main program /
- stack empty
- loop d -- mark d as visited
- insert(n) -- first node, mark visited
- while stack is not empty do
- pop m, the first element of stack, off
stack -- active node - for each predecessor p of m do insert(p) --
mark and push
2Take 8g3 what is the natural loop?
- Step1
- loop 3 stack empty
- Step 2
- loop 3, 8 stack 8
- Step 3
- loop 3, 8, 7 stack 7
- Step 4
- loop 3, 8, 7, 5, 6, 10 stack 5, 6, 10
- Step 5
- loop 3, 8, 7, 5, 6, 10, 4 stack 4, 6,
10 - Step 6
- loop 3, 8, 7, 5, 6, 10, 4 stack 4, 10
- Step 7
- loop 3, 8, 7, 5, 6, 10, 4 stack 8
- Step 8
- loop 3, 8, 7, 5, 6, 10, 4 stack
- Hence loop 3, 4, 5, 6, 7, 8, 10
1
2
3
4
5
6
7
8
8
10
3Finding Dominators
- Main Idea If p1, ..p2 are all the predecessors
of node n and - d ¹ n then d dom n if and only if d dom pi for
each i. - Input Flow graph G (N, E, no)
- Output The relation dom.
- Method Compute iteratively D(n), the set of
dominators of n. - D(no) no
- for n ÎN \ nodo D(n) N / end of
initialization / - while changes to any D(n) occur do
- for n ÎN \ nodo
- D(n) nU Ç D(p)
p Î pred(n)
Notes This is an example of Data-Flow Analysis
algorithm (later) -convergence D(n) cannot get
smaller indefinitely.
4Inner Loops and Pre-Headers
- Inner-loop A loop that contains no other loops
- Property of natural loops unless two loops have
the same header - - they are either disjoint or
- - one is entirely contained (nested within) the
other - - when two loops have the same header but
neither is nested within each other, they are
treated as a single loop - Preheader Necessary if we want to move
statements outside. - - successor The header
- - predecessors all edges which formerly entered
the header of L from outside L.
Bo
preheader
header
B1
header
B2
B3
loop L
loop L
Same header loops
5Reducible Flow Graphs
- Intuition Produced by exclusive use of flow of
control statements such as if-then-else,
while-do, continue, break etc. - Statistics Even programs written using goto
statements are almost always reducible. - Main property there are no jumps in the middle
of loops from outside the only entry to a loop
is through its header. - Definition A flow graph G is reducible if and
only if we can partition the edges into two
disjoint groups, often called the forward edges
and back edges with properties - - forward edges from an acyclic graph in which
every node can be reached from the initial node
of G. - - the back edges consist only of edges whose
heads dominate their tails.
6Example of Flow Graphs
7Example of Reducible Flow Graphs
- Knowing dom we can find and remove all the back
edges. - We get the forward edges.
- Check if they form an acyclic graph
8Example Reducible Flow Graphs
Consider the following flow graph with initial
node 1.
- Intuition This flow graph is not reducible
because the cycle 2 - 3 can be entered at two
different places, 2 and 3. - Key property of reducible flow graphs The set of
nodes informally regarded as a loop must contain
a back edge. - This flow graph has no back edges
- 2 dom 3 and 3 dom 2
- The loop 2 - 3 has two headers.
9Strongly Structured Reducible Flow Graphs
- Key Property Single entry point and single end
point - Generated from Structured Programs (grammar)
- S id E S S if E then S else S do S
while E - E id id id
- Result They can be analyzed as Abstract Syntax
Trees (AST)
S1
10Weakly Structured Reducible Flow Graphs
Example i m 1 j n a u1 do
i i 1 j j 1 if e3 then (d6)
a u2 else (d7) i u3
break while e2 Problem Jump at the
end of the loop (break) Conclusion AST
techniques are not naturally handled.
If-then-else DOES NOT have a single end point.
S1
do
e2
S2
if
e1
d6
break
d7
11Intervals of (Reducible) Flow Graphs
- Motivation Apply syntax-directed techniques in
a more general setting where the syntax does not
necessarily provide the structure but the flow
graph does. - Intuition Divide flow graphs into intervals that
put a hierarchical structure on the flow graph. - Intuitive Def A natural loop acyclic dangling
structure. - Property The header dominated all nodes in the
interval. - Formal Def Given a flow graph G (N, E, n0) and
a node n ? N, the interval with header n, I(n) is
defined by - n is in I(n)
- If predecessors(m) Í I(n) for m ¹ n0 then m ?
I(n) - Nothing else is in I(n).