Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- http//www.eecg.toronto.edu/voss/ece540/
- Lecture 03, Jan. 14, 2002
2My Contact Information
Michael Voss Office SF2002E Email
voss_at_eecg.toronto.edu Phone (416) 946
8031 Admin TBD, SF2002
3Your Information
- Name
- Email Address (I wont hunt you down)
- Department (ECE, CS, )
- Degree Objective
- Year (not 2002 ok, 1st 2nd etc..)
- (R)egistered, (A)uditing, (U)nsure
4Updated Grading Information
- Fighting the good fight
- Will let you know soon
5Control Flow Analysis Where do we go from here?
References Muchnick, Chapter 7. The Dragon
Book (Aho ), pp. 602-608.
6Purpose 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
7All 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!
8Basic 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 existed
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
9Basic 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
10Basic 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
11Basic 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
12Basic 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
13Basic 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
BB 1
BB 2
BB 3
BB 4
14Control 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
15Control 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.
16Control 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.
17Example (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
18Example (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
19Example (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
join nodes?
20Example (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
21Example (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
22Example (CFG)
Entry
branch node
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
BB 1
BB 2
BB 3
BB 4
BB 5
BB 6
BB 7
BB 8
BB 9
BB 10
Exit
23Example (CFG)
Entry
branch node
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
BB 1
BB 2
BB 3
BB 4
BB 5
BB 6
BB 7
BB 8
BB 9
BB 10
Exit
24Dominators
- 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.
25Dominators (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).
26Dominators (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.
27Algorithm 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
28Example
29Example
30Algorithm 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.
31Finding 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
32Algorithm 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
33Example (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
34Example (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
Entry 1,2,3 Þ Entry,1,2,3 1
Entry,2,3 Þ 1,2,3 2 1,3 Þ
2,3 3 2 Þ 3
35Post-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.
36Post-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).
37Post-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.
38Example (post-dominators)
39Example (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
40Lecture 4 January 17, 2001
41Grading Scheme
- Final Exam 35
- In-Class Midterm 25
- Project 40
- Control Flow Analysis 10
- Data Flow Analysis 10
- Optimizations 20
- 15 for working, 5 from competition
- Pending approval by ECE Exam Committee
42Accounts You Will Need
- ECF Unix Account (skule.ecf)
- If you need one, go to GB154
- ugsparc account
- If you need one, let me know your name and 9
digit student id - I will hand out the 1st assignment next week, so
be prepared with accounts
43Regarding Project Work
- All work must be done independently
- You can talk to each other
- Dont write anything down
- Dont copy files, cut and paste from files
- It is VERY unlikely that two people code anything
the same way - I will be automatically comparing your code with
other code that is turned in (including code
from previous offerings of this course).
44Control 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
45Finding Loops in the CFG
46Depth-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.
47Depth-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.
48Depth-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.
49Depth-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.
50Depth-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.
51Depth-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.
52Depth-first Traversal of Graphs
1
BB 1
2
BB 2
BB 3
BB 8
3
BB 4
BB 5
BB 8
4
BB 6
BB 7
BB 3
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.
53Depth-first Traversal of Graphs
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.
54Depth-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.
55Depth-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.
56Depth-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.
57Depth-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.
58Depth-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.
59Depth-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?)
60DFS 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)
61Depth-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.
62Loops
- 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.
63Natural 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
64Finding Loops
- find the dominators of each node in CFG.
- identify back edges.
- find the nodes of the loop associated with each
back edge.
65Finding 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.
66Reducible 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.
67Algorithm 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 pred(m)
insert_on_stack (p) insert_on_stack
(a) if (a Ï Loop) loop loop U
a push_on_stack (a)
For possibly irreducible CFGs, check for
predecessors outside of the set. If so, not
natural loop!
68Example (loops)
Iterate over all edges and use DT to determine
back edges. or Find retreating edges using
DFT. or Both (possibly irreducible CFGs)
69Example 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
70Example II (loops)
Entry
BB 1
Loop BB 3, BB 8
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 8
BB 8
BB 9
BB 10
Exit
71Example II (loops)
Entry
BB 1
Loop BB 3, BB 8
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 8
BB 9
BB 10
Exit
72Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 7
BB 8
BB 9
BB 10
Exit
73Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 8
BB 9
BB 10
Exit
74Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7, BB 5
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 5
BB 8
BB 9
BB 10
Exit
75Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7, BB 5, BB 6
BB 3
BB 2
BB 4
BB 5
BB 6
BB 6
BB 7
BB 5
BB 8
BB 9
BB 10
Exit
76Example II (loops)
Entry
BB 1
Loop BB 3, BB 8, BB 7, BB 5, BB 6
BB 3
BB 2
BB 4
BB 5
BB 6
BB 7
BB 5
BB 8
BB 9
BB 10
Exit
77Example 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 4
BB 5
BB 8
BB 9
BB 10
Exit
78Example 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 5
BB 8
BB 9
BB 10
Exit
79Example 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
80Irreducible CFG
- BB1 dom BB3
- entry at BB2
- not natural
- BB2 dom BB4
- natural loop
- rare in real apps
BB1
BB2
BB3
BB4
Reducible! Since for all retreating edges h dom
t. BB3 -gt BB1 is therefore natural.
81Control 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.
82Identifying 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.
Postorder DFS does not work!
83Loop 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.
84Nested (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