![Page 1: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/1.jpg)
1
Code Optimization
Chapter 9���(1st ed. Ch.10)
COP5621 Compiler Construction���Copyright Robert van Engelen, Florida State University, 2007-2013
![Page 2: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/2.jpg)
2
The Code Optimizer
• Control flow analysis: control flow graph • Data-flow analysis • Transformations
Front���end
Code���generator
Code���optimizer
Control-���flow ���
analysis
Data-���flow ���
analysis
Transfor-���mations
![Page 3: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/3.jpg)
3
Determining Loops in Flow Graphs: Dominators
• Dominators: d dom n – Node d of a CFG dominates node n if every path from
the initial node of the CFG to n goes through d – The loop entry dominates all nodes in the loop
• The immediate dominator m of a node n is the last dominator on the path from the initial node to n – If d ≠ n and d dom n then d dom m
![Page 4: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/4.jpg)
4
Dominator Trees
1 2
3
4 5 6
7
8 9 10
1
2 3
4
6 5 7
8
9 10
CFG Dominator tree
![Page 5: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/5.jpg)
5
Natural Loops • A back edge is is an edge a → b whose head b
dominates its tail a • Given a back edge n → d
– The natural loop consists of d plus the nodes that can reach n without going through d
– The loop header is node d • In other words
– A natural loop must have a single-entry node d – There must be a back edge that enters node d
![Page 6: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/6.jpg)
6
Natural Inner/Outer Loops • Unless two loops have the same header, they are
disjoint or one is nested within the other • A nested loop is an inner loop if it contains
no other loops • A loop is an outer loop if it is not contained
within another loop
![Page 7: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/7.jpg)
7
Natural Inner Loops Example
1 2
3
4 5 6
7
8 9 10
1
2 3
4
6 5 7
8
9 10
CFG Dominator tree
Natural loop���for 7 dom 10
Natural loop���for 3 dom 4
Natural loop���for 4 dom 7
![Page 8: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/8.jpg)
8
Natural Outer Loops Example
1 2
3
4 5 6
7
8 9 10
1
2 3
4
6 5 7
8
9 10
CFG Dominator tree
Natural loop���for 1 dom 9
Natural loop���for 3 dom 8
![Page 9: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/9.jpg)
9
Pre-Headers
• To facilitate loop transformations, a compiler often adds a preheader to a loop
• Code motion, strength reduction, and other loop transformations populate the preheader
Header Header
Preheader
![Page 10: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/10.jpg)
10
Reducible Flow Graphs
1 2
3
4
Example of a���reducible CFG
1
2 3
Example of a���nonreducible CFG ���
(not a natural loop: no back edge to dominator 1)
• Reducible graph = disjoint partition in forward and back edges such that the forward edges form an acyclic (sub)graph
![Page 11: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/11.jpg)
11
Global Data-Flow Analysis • To apply global optimizations on basic blocks,
data-flow information is collected by solving systems of data-flow equations
• Suppose we need to determine the live-variables for a sequence of statements S���
in[S] = use[S] ∪ (out[S] - def[S])
i := m-1 j := n
j := j-1
:= i+j
B1:
B2:
B3:
Solution:���in[B1] = {m, n}∪({i, j}–{i, j}) = {m, n}���out[B1] = in[B2] = {i, j}���in[B2] = {j} ∪ ({i, j} – {j}) = {i, j} out[B2] = in[B3] = {i, j}
![Page 12: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/12.jpg)
12
Live-Variable Analysis
S d: a:=b+c
Then, the data-flow equations for S are:������use[S] = {b, c}���def[S] = {a}���in[S] = use[S] ∪ (out[S] – def[S])���
is of the form
out[S]
in[S]
applies ���transfer function:���f[S](x) = use[S] ∪ (x - def[S])
backwards
![Page 13: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/13.jpg)
13
Live-Variable Analysis
S
������in[S] = in[S1]���in[S1] = use[S1] ∪ (out[S1] – def[S1]) out[S1] = in[S2]���in[S2] = use[S2] ∪ (out[S2] – def[S2])���out[S2] = out[S]
is of the form
S2
S1
![Page 14: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/14.jpg)
14
Live-Variable Analysis
S
���in[S] = in[S1] ∪ in[S2]���in[S1] = use[S1] ∪ (out[S1] – def[S1])���in[S2] = use[S2] ∪ (out[S2] – def[S2])���out[S1] = out[S] ���out[S2] = out[S]
is of the form S2 S1
![Page 15: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/15.jpg)
15
Live-Variable Analysis
S
������in[S] = in[S1] ���in[S1] = use[S1] ∪ (out[S1] – def[S1]) out[S1] = out[S] ∪ use[S1]
is of the form S1
non-iterative solution
![Page 16: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/16.jpg)
16
Global Data-Flow Analysis • Suppose we need to determine the reaching
definitions for a sequence of statements S��� out[S] = gen[S] ∪ (in[S] - kill[S])
d1: i := m-1 d2: j := n
d3: j := j-1
B1:
B2:
B3:
out[B1] = gen[B1] = {d1, d2}���out[B2] = gen[B2] ∪ {d1} = {d1, d3}��� d1 reaches B2 and B3 and d2 reaches B2, but not B3���because d2 is killed in B2
![Page 17: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/17.jpg)
17
Reaching Definitions
S d: a:=b+c
Then, the data-flow equations for S are:������gen[S] = {d}���kill[S] = Da - {d}���out[S] = gen[S] ∪ (in[S] - kill[S]) where Da = all definitions of a in the region of code
is of the form
applies ���transfer function:���f[S](x) = gen[S] ∪ (x - kill[S])
out[S]
in[S]
forwards
![Page 18: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/18.jpg)
18
Reaching Definitions
S
������gen[S] = gen[S2] ∪ (gen[S1] - kill[S2])���kill[S] = kill[S2] ∪ (kill[S1] - gen[S2])���in[S1] = in[S]���in[S2] = out[S1] out[S] = out[S2]
is of the form
S2
S1
![Page 19: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/19.jpg)
19
Reaching Definitions
S
������gen[S] = gen[S1] ∪ gen[S2] ���kill[S] = kill[S1] ∩ kill[S2]���in[S1] = in[S]���in[S2] = in[S] out[S] = out[S1] ∪ out[S2]
is of the form S2 S1
![Page 20: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/20.jpg)
20
Reaching Definitions
S
������gen[S] = gen[S1] ���kill[S] = kill[S1]���in[S1] = in[S] ∪ gen[S1] out[S] = out[S1]
is of the form S1
non-iterative solution ���(see later)
![Page 21: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/21.jpg)
21
Reaching Definitions:���Computing Gen/Kill
d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u2 else d7: i := u3 while e2
;
gen={d1}���kill={d4, d7}
d1 gen={d2}���kill={d5}
d2
gen={d1,d2}���kill={d4,d5,d7}
;
d3 gen={d3}���kill={d6}
gen={d1,d2,d3}���kill={d4,d5,d6,d7}
; gen={d3,d4,d5,d6,d7}���kill={d1,d2}
do
;
gen={d4}���kill={d1, d7}
d4
;
gen={d5}���kill={d2}
d5
if
e2
d6 d7 e1 gen={d6}���kill={d3}
gen={d7}���kill={d1,d4}
gen={d4,d5}���kill={d1,d2,d7}
gen={d4,d5,d6,d7}���kill={d1,d2}
gen={d4,d5,d6,d7}���kill={d1,d2}
gen={d6,d7}���kill={}
![Page 22: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/22.jpg)
22
Using Bit-Vectors to Compute Reaching Definitions
d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u2 else d7: i := u3 while e2
;
d1 d2
;
d3
; 0011111 1100000
do
;
d4
;
d5
if
e2
d6 d7 e1
1110000 0001111
1100000 0001101
1000000 0001001
0100000 0000100
0010000 0000010
0001111 1100000
0001111 1100000
0001100 1100001
0001000 1000001
0000100 0100000
0000010 0010000
0000001 1001000
0000011 0000000
d1 d2 d3 d4 d5 d6 d7 gen=���kill=
![Page 23: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/23.jpg)
23
Reaching Definitions:���Computing In/Out (non-iterative)
d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u2 else d7: i := u3 while e2
;
in={}���out={d1}
d1 in={d1}���
out={d1,d2} d2
in={}���out={d1,d2}
;
d3 in={d1,d2}���
out={d1,d2,d3}
in={}���out={d1,d2,d3}
; in={}���out={d3,d4,d5,d6,d7}
do
;
in={d1,d2,d3,d4,d5,d6,d7}���out={d2,d3,d4,d5,d6}
d4
;
in={d2,d3,���d4,d5,d6}���
out={d3,d4,d5,d6} d5
if
e2
d6 d7 e1 in={d3,d4,��� d5,d6}���out={d4,d5,d6}
in={d3,d4,d5,d6}���out={d3,d5,��� d6,d7}
in={d1,d2,d3,d4,d5,d6,d7}���out={d3,d4,d5,d6}
in={d1,d2,d3,���d4,d5,d6,d7}���
out={d3,d5,d6,d7}
in={d1,d2,d3}���out={d3,d4,d5,���
d6,d7}
in={d3,d4,d5,d6}���out={d3,d4,d5,d6,d7}
![Page 24: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/24.jpg)
24
Accuracy, Safeness, and Conservative Estimations
• Conservative: refers to making safe assumptions when insufficient information is available at compile time, i.e. the compiler has to guarantee not to change the meaning of the optimized code
• Safe: refers to the fact that a superset of reaching definitions is safe (some may have been killed)
• Accuracy: more and better information enables more code optimizations
![Page 25: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/25.jpg)
25
Reaching Definitions are a Conservative (Safe) Estimation
S2 S1
Suppose this ���branch is ���
never taken
Estimation: ���gen[S] = gen[S1] ∪ gen[S2] ���kill[S] = kill[S1] ∩ kill[S2]������Accurate: ���gen’[S] = gen[S1] ⊆ gen[S] ���kill’[S] = kill[S1] ⊇ kill[S]
![Page 26: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/26.jpg)
26
Reaching Definitions are a Conservative (Safe) Estimation
in[S1] = in[S] ∪ gen[S1] S1
Why gen?
S is of the form
The problem is that������
in[S1] = in[S] ∪ out[S1]������
makes more sense, but we cannot solve this ��� directly (only iteratively), because out[S1] depends on in[S1]
![Page 27: Code Optimization - Florida State Universityengelen/courses/COP5621-2013/Ch10.pdf5! Natural Loops! • A back edge is is an edge a → b whose head b dominates its tail a! • Given](https://reader035.vdocument.in/reader035/viewer/2022081623/61495b0c080bfa6260148e8d/html5/thumbnails/27.jpg)
27
Reaching Definitions are a Conservative (Safe) Estimation
d: a:=b+c We have: (1) in[S1] = in[S] ∪ out[S1] (2) out[S1] = gen[S1] ∪ (in[S1] - kill[S1])���
Solve in[S1] and out[S1] by estimating in1[S1] using safe but���approximate out[S1]=∅, then re-compute out1[S1] using (2) to estimate in2[S1], etc. in1[S1] =(1) in[S] ∪ out[S1] = in[S]���out1[S1] =(2) gen[S1] ∪ (in1[S1] - kill[S1]) = gen[S1] ∪ (in[S] - kill[S1])���in2[S1] =(1) in[S] ∪ out1[S1] = in[S] ∪ gen[S1] ∪ (in[S] - kill[S1]) = in[S] ∪ gen[S1] ���out2[S1] =(2) gen[S1] ∪ (in2[S1] - kill[S1]) = gen[S1] ∪ (in[S] ∪ gen[S1] - kill[S1])���
= gen[S1] ∪ (in[S] - kill[S1]) ������Because out1[S1] = out2[S1], and therefore in3[S1] = in2[S1], we conclude that���
in[S1] = in[S] ∪ gen[S1]