Roadmap 2 Last time Path problems Shortestdistance problem Started on concrete semantics Showing you the how but not the why Today the why Via an optimization example 3 ID: 807139
Download The PPT/PDF document "An Optimization Example 1" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
An Optimization Example
1
Slide2Roadmap
2
Last time
Path problems
Shortest-distance problem
Started on concrete semantics
Showing you the “how,” but not the “why”
Today: the “why”
Via an optimization example
Slide33
Quicksort
*
*
Example from
Aho
,
Sethi, Ullman, Compilers: Principles, Techniques, and Tools, Addison-Wesley, 1986
void quicksort(
int
m,
int
n) {
int
i
, j, v, x;
if (n <= m) return;
i
= m-1; j = n; v = a[n];
while(1) {
do
i
= i+1; while (a[
i
] < v);
do j = j-1; while (a[j] > v);
if (
i
>= j) break;
x = a[
i
]; a[
i
] = a[j]; a[j] = x;
}
x = a[
i
]; a[
i
] = a[n]; a[n] = x;
quicksort(
m,j
); quicksort(i+1,n);
}
Slide4Three-address code (IR)
Roughly:
≈assembly code but an unbounded number of registers no explicit manipulation of the stack, ARs, etc.Types of 3-address statementsx := y copy statementsx := op y unary opx := y op z binary opx := y[i] indexed fetch
x[
i
] := y indexed copy
goto P gotox := &y assign address
x := *y assign indirect
*x := y store indirect
if x relop y goto P if-thenparam x1; param x2; …; param xn; call p,n callreturn y return
4
Slide5Three-address code (IR)
t1 := -c
t2 := b * t1
t3 := -ct4 := b * t3t5 := t2 + t4a := t55AssignPlus
Times
Times
UMinus
UMinus
Id[a]
Id[b]
Id[c]
Id[c]
Id[b]
t1
t3
t4
t2
t5
Slide66
Quicksort
void quicksort(
int
m,
int
n) {
int i, j, v, x;
if (n <= m) return;
i = m-1; j = n; v = a[n]; while(1) { do i = i+1; while (a[i] < v); do j = j-1; while (a[j] > v); if (i >= j) break; x = a[i]; a[i] = a[j]; a[j] = x; } x = a[i]; a[i] = a[n]; a[n] = x; quicksort(m,j); quicksort(i+1,n);}
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]
if t3 < v
goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
t6 := 4*ix := a[t6]
t7 := 4*i
t8 := 4*jt9 := a[t8]a[t7] := t9
t10 := 4*ja[t10] := xgoto
(5)
t11 := 4*i
x := a[t11]t12 := 4*it13 := 4*nt14 := a[t13]a[t12] = t14t15 := 4*na[t15] := x
Slide7Scope of transformations
Local
transformations within a single basic block
Intraproceduraltransformations within a single procedureInterproceduraltransformation across procedure boundaries7
Slide8Kinds of optimizing transformations
Remove redundant or useless computations
common-subexpression elimination
copy propagationunreachable-code eliminationconstant foldingLoop optimizationscode motion (move code outside loop)reduction in strength8
Slide9Regardless of level
Loop until done:
Analyze program to gather “facts”
Apply transformationMore preciselyAnalyze program to gather “facts”Loop until done: Apply transformation Update facts to reflect the new state of the code9
Analyze program
perform dataflow analysis on the program’s CFG
What kinds of analyses?
Possible values of variables
v is a constant k
v points only to vars in set Sfunction pointer f can only hold the address of a procedure in set Plabel var l can only hold labels in set Lcan pointer p be NULL at point p?what are upper/lower bounds on the value of x at point p?does x == y hold at point p?
Other execution properties
var
v may/may not be used subsequently (live/dead)
value assigned at def-site d: x = … may be used at use-site u: … x … (d reaches u)expression e has always already been computed (“is available”) whenever execution reaches point p
can there be any overflows in the computation at point p
10
Slide1111
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
t6 := 4*
i
x := a[t6]
t7 := 4*
i
t8 := 4*j
t9 := a[t8]
a[t7] := t9
t10 := 4*j
a[t10] := xgoto (5)
t11 := 4*
ix := a[t11]t12 := 4*i
t13 := 4*nt14 := a[t13]
a[t12] = t14t15 := 4*na[t15] := x
Slide1212
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
t6 := 4*
i
x := a[t6]
t8 := 4*j
t9 := a[t8]
a[
t6
] := t9
a[t8] := xgoto (5)
t11 := 4*
ix := a[t11]
t12 := 4*it13 := 4*nt14 := a[t13]
a[t12] = t14t15 := 4*n
a[t15] := x
Slide1313
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
x :=
t3
a[
t2
] := t5
a[t4] := xgoto (5)
t11 := 4*
ix := a[t11]t12 := 4*i
t13 := 4*nt14 := a[t13]a[t12] = t14
t15 := 4*na[t15] := x
Copy
propagation
Slide1414
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
x := t3
a[t2] := t5
a[t4] :=
t3
goto (5)
t11 := 4*i
x := a[t11]t12 := 4*it13 := 4*n
t14 := a[t13]a[t12] = t14t15 := 4*n
a[t15] := x
x always defined
before (re)used
“x is dead”
Slide1515
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
a[t2] := t5
a[t4] := t3
goto
(5)
t11 := 4*ix := a[t11]
t12 := 4*it13 := 4*n
t14 := a[t13]a[t12] = t14t15 := 4*na[t15] := x
Slide16Dead-code elimination
A variable is
live
at a point p if its value can be used subsequently. A variable that is not live is dead.Idea: remove assignments to dead variables16p: x := …if …
goto
q
r: …
x := …
q: …
… x …
x is live just after p
Slide1717
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
a[t2] := t5
a[t4] := t3
goto
(5)
t11 := 4*ix := a[t11]
t12 := 4*it13 := 4*n
t14 := a[t13]a[t12] = t14t15 := 4*na[t15] := x
Reduction in strength:
replace * with +
E.g., replace
j := j-1
t4 := 4*j
with
j := j-1 t4 := t4 - 4
Slide1818
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
i
:= i+1
t2 := 4*
i
t3 := a[t2]if t3 < v goto
(5)
j := j-1
t4 :=
t4 - 4
t5 :- a[t4]
if t5 > v
goto
(9)
if
i
>= j
goto
(23)
a[t2] := t5
a[t4] := t3
goto (5)
t11 := 4*i
x := a[t11]t12 := 4*it13 := 4*n
t14 := a[t13]a[t12] = t14t15 := 4*n
a[t15] := x
Reduction in strength:
replace * with +
E.g., replace
j := j-1
t4 := 4*j
with j := j-1 t4 := t4 - 4t4 := 4*j
Slide19i
:= m-1
j := n
t1 := 4*nv := a[t1]i := i+1t2 := 4*i
t3 := a[t2]
if t3 < v
goto
(5)
j := j-1
t4 := 4*j
t5 :- a[t4]if t5 > v goto (9)if
i
>= j
goto
(23)
t6 := 4*
i
x := a[t6]
t7 := 4*
i
t8 := 4*j
t9 := a[t8]
a[t7] := t9
t10 := 4*j
a[t10] := x
goto
(5)
t11 := 4*
i
x := a[t11]
t12 := 4*
it13 := 4*n
t14 := a[t13]a[t12] = t14t15 := 4*na[t15] := x
i
:= m-1
j := n
t1 := 4*n
v := a[t1]
t2 := 4*
i
t4 := 4*j
t2 := t2+4
t3 := a[t2]if t3 < v goto (5)t4 := t4 - 4t5 :- a[t4]if t5 > v goto (9)if t2 >= t4 goto (23)
a[t2] := t5a[t4] := t3goto (5)t14 := a[t1]a[t2] = t14a[t1] := t3