Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- Register and login to CCNET for more
- Control Flow Analysis, Jan. 11, 2005
2Control Flow Analysis Where do we go from here?
References Muchnick, Chapter 7. The Dragon
Book (Aho ), pp. 602-608.
3Purpose of Control Flow Analysis
- Determine the control structure of a program
- determine possible control flow paths
- find basic blocks and loops
- Intraprocedural within a procedure
- Interprocedural across procedures
- Whole program
- Maybe just within the same file
cc c file1.c cc c file2.c cc o myprogram
file1.o file2.o -l mylib
4All about Control flow analysis
- Finding basic blocks
- Creating a control flow graph
- Finding dominators
- dominators, proper dominators, direct dominators
- Finding post-dominators
- Finding loops
Youll need to do this for the first assignment!
5Basic Blocks
- A Basic Block (BB) is a maximal section of
straight-line code which can only be entered
via the first instruction and can only be exited
via the last instruction.
S1 read L S2 n 0 S3 k 0 S4 m 1 S5 k k
m S6 c k gt L S7 if (c) goto S11 S8 n n
1 S9 m m 2 S10 goto S5 S11 write n
6Basic Blocks (continued)
- Identify basic blocks in two steps
- Identify leader instructions in the program
- entry point of the program.
- target of branch instructions.
- instructions immediately following branch
instructions. - Each leader and all instructions that follow it
up to but not including the next leader or exit
constitute one BB.
S1 read L S2 n 0 S3 k 0 S4 m 1 S5 k k
m S6 c k gt L S7 if (c) goto S11 S8 n n
1 S9 m m 2 S10 goto S5 S11 write n
7Control Flow Graphs
- The Control Flow Graph (CFG) of a program is a
directed graph G(N, E) whose nodes N represent
the basic blocks in the program and whose edges E
represent transfers of control between basic
blocks.
S1 read L S2 n 0 S3 k 0 S4 m 1 S5 k k
m S6 c k gt L S7 if (c) goto S11 S8 n n
1 S9 m m 2 S10 goto S5 S11 write n
BB 1
BB 2
BB 3
BB 4
8Control Flow Graphs (continued)
- Given G (N, E) and a basic block b Î N.
- The successors of b, denoted by succ(b), is the
set of basic blocks that can be reached from b by
traversing one edge succ(b) n Î N
(b,n) Î E - The predecessors of b, denoted by pred(b), is the
set of basic blocks that can reach b by
traversing one edge pred(b) m Î N
(m,b) Î E
- An entry node in G is one which has no
predecessors. - An exit node in G is one which has no successors.
9Control Flow Graphs (continued)
- A branch node in G is one which has more than one
successor. - A join node is one which has more than one
predecessor.
- CFGs are usually sparse E O(N). Indeed,
when only binary branching is allowed, E 2N.
10Example (CFG)
i 1 L1 if (i gt n) goto L6 t1 0 j
1 L2 if (j gt n) goto L5 tmp tets if (tmp lt
0) goto L3 t1 t1 ts goto L4 L3 t1 t1
te L4 j j1 goto L2 L5 i i 1 goto
L1 L6 t1 0
11Dominators
- Let G(N, E) denote a CFG. Let n, n Î N.
- n is said to dominate n, denoted n dom n, iff
every path from Entry to n contains n.1 dom 1
1 dom 2 1 dom 3 1 dom 4 2
dom 2 2 dom 3 2 dom 4 3 dom 3
4 dom 4. - n is said to properly dominate n, denoted by n
domp n, iff n dom n and n ¹ n.1 domp 2 1
domp 3 1 domp 4 2 domp 3 2 domp 4. - n is said to directly dominate n, denoted by n
domd n, iff n domp n and there is no n b N
such that n domp n domp n.1 domd 2 2
domd 3 2 domd 4.
12Dominators (continued)
- The set of (proper/direct) dominators of a node n
is the set of CFG nodes that (properly/directly)
dominate n. That is DOM
(n) m m dom n DOMp(n)
m m domp n DOMd(n)
m m domd n - The set of dominators of a node is unique.
(Why?). - The direct dominator of a node is unique. (Why?).
- The dominance relation is a partial ordering
that is, it is reflexive, anti-symmetric and
transitive - reflexive x dom x.
- anti-symmetric x dom y and y dom x implies x
y. - transitive x dom y and y dom z implies x dom z.
- (Prove as an exercise).
13Dominators (continued)
- The relation domd can be represented by a tree
called the dominator tree (DT) whose root is
Entry, whose nodes are N, and whose edges are
direct dominance relations. - The set of dominators of a node are on the path
from the node to the root of the DT.
14Algorithm for Finding Dominators
- DOM(Entry) EntryDOM(v) N " v Î N - Entry,
Exitchanged true - while (changed) changed false
for each v Î N - Entry, Exit
oldDOM DOM(v) - if (DOM(v) ! oldDOM)
changed true
15Example
16Algorithm for Finding Dominators
- Will this algorithm terminate? Why?
- This algorithm is not the most efficient for
large CFGs. An almost linear-time algorithm can
be found inT. Lengauer and R. Tarjan, A Fast
Algorithm for Finding Dominators in a Flowgraph,
ACM Transactions on Programming Languages and
Systems, vol. 1, no. 1, pages 121-142, July,
1979. - Also, algorithms exist for incrementally
re-building dominance relations. See Alstrup
and P. Lauridsen, A Simple Dynamic Algorithm for
Maintaining a Dominator Tree, Technical Report
96/3, Dept. of Computer Science, University of
Copenhagen, Copenhagen, 1996.
17Finding Direct Dominators
- The direct dominator of a node n is the proper
dominator of n that does not properly dominate
any other proper dominator of n - Initialize a set with the proper dominators of a
node - Remove from the set nodes that properly dominate
other nodes in this set, for they cannot be the
direct dominator!
DOMd (1) Entry DOMd (2) 1 DOMd (3)
2 DOMd (4) 2
18Algorithm for Finding Direct Dominators
- Initialize a set with the proper dominators of a
node - Remove from the set nodes that properly dominate
other nodes in this set, for they cannot be the
direct dominator!
for each n Î N-Entry DOMd(n) DOM(n)
- n for each n Î N-Entry for each
s Î DOMd(n) for each t Î DOMd(n) -
s if (t Î DOMd(s))
DOMd(n) DOMd(n) - t
19Example (dominators)
DOM (Entry) Entry DOM (1)
Entry,1 DOM (2) Entry,1,2 DOM (3)
Entry,1,2,3 DOM (4) Entry,1,2,3,4 DOM
(5) Entry,1,2,3,4,5 DOM (6)
Entry,1,2,3,4,5,6 DOM (7)
Entry,1,2,3,4,5,7 DOM (8)
Entry,1,2,3,4,5,8 DOM (9)
Entry,1,2,3,4,9 DOM (10) Entry,1,2,10)
Lets find the direct dominators
20Example (direct dominators)
DOMd (1) Entry DOMd (2) Entry,1 DOMd
(3) Entry,1,2 DOMd (4) Entry,1,2,3 DOMd
(5) Entry,1,2,3,4 DOMd (6) Entry,1,2,3,4,5
DOMd (7) Entry,1,2,3,4,5 DOMd
(8) Entry,1,2,3,4,5 DOMd (9) Entry,1,2,3,4
DOMd (10) Entry,1,2)
Entry
BB 1
BB 2
BB 3
BB 10
BB 4
BB 5
BB 9
DOMd (1) Entry DOMd (2) 1 DOMd
(3) 2 DOMd (4) 3 DOMd (5) 4 DOMd
(6) 5 DOMd (7) 5 DOMd (8) 5 DOMd
(9) 4 DOMd (10) 2)
DOMd (1) Entry DOMd (2) 1 DOMd
(3) 2 DOMd (4) 3 DOMd (5) 4 DOMd
(6) 5 DOMd (7) 5 DOMd (8) 5 DOMd
(9) 4 DOMd (10) 2)
BB 7
BB 6
BB 8
21Post-Dominators
- Let G(N, E) denote a CFG. Let n, n Î N. Then
- n is said to post-dominate n, denoted n pdom
n, iff every path from n to Exit contains n.1
pdom 1 2 pdom 1 4 pdom 1 2 pdom 2 4
pdom 2 3 pdom 3 2 pdom 3 4 pdom 3 - 4 pdom 4.
- n is said to properly post-dominate n, denoted
by n pdomp n, iff n pdom n and n ¹ n.2
pdomp 1 4 pdomp 1 4 pdomp 2 2 pdom 3 - 4 pdomp 3.
- n is said to directly post-dominate n, denoted
by n pdomd n, iff n pdomp n and there is no
n Î N such that n pdomp n pdomp n.2 pdomd
1 2 pdomd 3 4 pdomd 2.
22Post-Dominators (continued)
- The set of (proper/direct) post-dominators of a
node n is the set of CFG nodes that
(properly/directly) post-dominate n. That is
PDOM(n) m m pdom n
PDOMp(n) m m pdomp n
PDOMd(n) m m pdomd n - The set of post-dominators is unique. (Why?).
- The direct post-dominator of every node is
unique. (Why?). - The post-dominance relation is also a partial
ordering that is, it is reflexive,
anti-symmetric and transitive - reflexive x pdom x.
- anti-symmetric x pdom y and y pdom x implies x
y. - transitive x pdom y and y pdom z implies x pdom
z. - (Prove as an exercise).
23Post-Dominators (continued)
- The relation pdomd can be represented by a tree
called the post-dominator tree (PDT) whose root
is Exit, nodes are N and edges are direct
post-dominance relations. - The set of post-dominators of a node are on the
path from the node to the root of the PDT.
- Post-dominators in G are dominators in G-1.
24Example (post-dominators)
25Example (post-dominators)
PDOM (10) Exit,10 PDOM (9)
Exit,10,2,9 PDOM (8) Exit,10,2,9,4,8 PDO
M (7) Exit,10,2,9,4,8,7 PDOM (6)
Exit,10,2,9,4,8,6 PDOM (5)
Exit,10,2,9,4,8,5 PDOM (4)
Exit,10,2,9,4 PDOM (3) Exit,10,2,9,4,3 P
DOM (2) Exit,10,2 PDOM (1)
Exit,10,2,1)
Make sure you can find this
26Control Flow Review
- How to find basic blocks
- How to construct a control flow graph
- Dominators, proper dominators, direct dominators
- Post-dominators, proper post-dominators, direct
post-dominators - dominators in G-1 are post-dominators in G
27Finding Loops in the CFG
28Depth-first Traversal of Graphs
BB 1
BB 2
BB 3
BB 4
BB 5
BB 6
BB 7
BB 8
- A traversal of a graph visits each node once and
only once. - Many traversal orders (depth-first,
breadth-first, minimum cost, etc). - A Depth-first traversal visits the descendants of
a node before visiting its siblings. - It is performed using a stack.
29Depth-first Traversal of Graphs
- A traversal of a graph visits each node once and
only once. - Many traversal orders (depth-first,
breadth-first, minimum cost, etc). - A Depth-first traversal visits the descendants of
a node before visiting its siblings. - It is performed using a stack.
- It is not unique! (why?)
30DFS can use call stack
struct node struct node left struct node
right int preorder int postorder int
pre_cnt 0, post_cnt 0 void visit (struct
node n) if (n NULL n-gtpreorder ! 0)
return n-gtpreorder pre_cnt visit(
n-gtleft ) visit( n-gtright ) n-gtpostorder
post_cnt void dfs() visit(root_node)
31Depth-first Traversal of Graphs
1
BB 1
An ancestor in the DFST always has a smaller
order number than its descendant.
2
6
BB 2
BB 3
A node with a smaller order number is not
necessarily a ancestor of one with a higher order
number.
3
7
BB 4
BB 5
4
8
BB 6
BB 7
To process an ancestor before a descendant,
process the nodes in their depth-first order.
5
BB 8
- Depth-first spanning tree (DFST).
- Depth-first order of nodes.
32Loops
- Goal find loops in CFG irrespective of input
syntax - DO, while, for, goto, etc.
- Intuitively, a loop is the set of nodes in a CFG
that form a cycle. - However, not every cycle is a loop.
- A natural loop has a single entry node h Î N
and a tail node t Î N, such that (t,h) Î E loop
can be entered only through h the loop contains
h and all nodes that can reach t without going
through h.
33Natural Loops
- A natural loop must have a single entry point,
called the header. - this entry point dominates all nodes in the loop
(why?) - There must be at least one way to iterate the
loop - i.e. at least one path back to the header
- We find natural loops by finding back edges
- back edge e (t,h) Î E where h dom t
- Given a back edge e, we define the natural loop
of the edge to be h plus the set of all nodes
that can reach t without going through h. - Natural loops are suitable for improvement
34Finding Loops
- find the dominators of each node in CFG.
- identify back edges.
- find the nodes of the loop associated with each
back edge.
Note In a reducible CFG, you can find back-edges
without finding dominators. Any retreating edge
is a back-edge. However, the above approach
works for all programs.
35Finding Back Edges
- Perform depth-first traversal of CFG (N,E).
- A retreating edge e (t,h) Î E is a back edge if
h dom t. - A retreating edge is always a back edge in a loop
written using structured constructs (i.e., no
gotos). The CFGs of such programs are said to
be reducible graphs. - One can use gotos to obtain irreducible flow
graphs. - We will only consider reducible CFGs , so we can
just look for a retreating edge e (t,h) Î E. If
(pre-order DFS of h) lt (pre-order DFS of t),
then the edge is a retreating edge and a
back-edge.
36Reducible Graphs
- The forward edges form an acyclic graph in which
every node can be reached from the initial node
of G. - The retreating edges consist only of edges whose
heads dominate their tails (back edges). - Exclusive use of structured control-flow
statements produce programs with reducible CFGs - such as if-then-else, while-do, continue, break
- Programs written using goto statements by
programmers with no prior knowledge of structured
program design are almost always reducible.
37Algorithm for Finding the Nodes in the Loop
Stack S Empty Set Loop h insert_on_stack
(t) while (S is not empty) m pop (S)
for each p ?pred(m)
insert_on_stack (p) insert_on_stack
(a) if (a Ï Loop) loop loop U
a push_on_stack (a)
38Example (loops)
Iterate over all edges and use DT to determine
back edges. or Find retreating edges using
DFT. or Both (possibly irreducible CFGs)
39Example II (loops)
Entry
BB 1
h
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
t
BB 8
BB 9
BB 10
Exit
40Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7, BB 5, BB
6, BB 4
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 8
BB 9
BB 10
Done !
Exit
41Control Flow Analysis Review
- Found Basic Blocks, CFG, Dominators,
Post-Dominators. - Found natural loops by finding back-edges.
- In a reducible control flow graph back-edges
define natural loops. - Irreducible graphs are rare in practice and so
can be mostly ignored.
42Identifying Non-Natural Loops in Irreducible
Graphs
Vugranam C. Sreedhar and Guang R. Gao and
Yong-Fong Lee, Identifying loops using DJ
graphs, ACM Transactions on Programming
Languages and Systems, 18(6), pages 649-658, 1996.
43Loop Pre-Headers
- Several optimizations require that code be moved
before the header. - It is convenient to create a new block called the
pre-header. - The pre-header has only the header as successor.
- All edges that formerly entered the header
instead enter the pre-header, with the exception
of edges from inside the loop.
44Nested (Natural) Loops
- If two loops have different headers then either
one must be nested within the other, or they are
disjoint (why?). - The header of the outer loop dominates the header
of the inner loop. - The innermost loop is the one that contains no
other loops. - If two loops share the same header, then it is
difficult to tell which is the inner loop. In
this case, they are treated as one loop for
optimization purposes.
i 1 BB 1 if ((i 10) 0)
goto BB 3 elsif (i gt 100)
goto out BB 2 ---- i goto BB 1 BB
3 ---- i goto BB 1 out
BB 1 if (i lt j) goto BB 2 elsif (i gt j) goto
BB 3 else goto out BB
2 ---- i goto BB 1 BB 3 ---- i-- goto
BB 1 out