optimization compiler baojian hua [email protected]
TRANSCRIPT
Middle End
AST translation IR1
asmother IR
and translation
translation IR2
Optimizations
AST translation IR1
asmother IR
and translation
translation IR2
opt
optopt
opt
opt
Optimizationin General
Optimization Optimization is a (semantics
preserving) code rewriting (transformation) such that the code after the transformation: is smaller is faster use less memory is more power efficient …
Optimization in practice Optimization is a code rewriting (transform
ation) such that the code after the transformation: is smaller is faster use less memory is more power efficient …(for some inputs, on some machines, on some OS
es, with particular cache size, with particular processes running, …)
Full Optimization is impossible
Could solve the halting problem: Compare Opt(p) with:
L: jmp L This is the famous fully employment t
heorem for compiler writers.
Optimization is difficult
No optimization always produces “better” result
Usually undecidable Correctness can be very subtle
A lot of research, in recent years, try to formally verify the correctness of optimizations
Caveat
Try to do good things most of the time not all programs are equally likely to be
seen, right? invent optimizations which work for most of
them for most of time
Don’t expect a perfect compiler if a compiler know enough optimization
tricks, we deem it mature
Today’s topics Early optimizations
local, flow-insensitive so don’t require analysis (almost) constant folding, algebraic simplifications, dea
d-code eliminations, etc. Flow-sensitive optimization
based on the result of (data-flow) analysis constant propagation, copy propagation, comm
on-subexpression elimination (CSE), dead-code elimination, etc.
Constant Folding
Constant Folding
Basic idea: calculate expressions known to be const
ants at compile-time e.g.: a = 3 + 5 ==> a = 8 e.g.: if (true && false) … ==> if (false)
Easy on integers or booleans, also possible on floats (but complicated)
Moral Easy to implement, can be performed on AS
T or low-level IRs Often implemented as a common subroutin
e to be called whenever desired Must be very careful with languages semant
ics overflow or exceptions
e.g.: 0xffffffff+1 ==> 0 (???)
Algebraic Simplification
Constant Folding Basic idea:
Make use of algebraic properties to simplify expressions
e.g.: a = 0+b ==> a = b e.g.: a = 1*b ==> a = b e.g.: 2*a ==> a + a e.g.: 2*a ==> a<<1 (strength reduction) e.g.: *(&a) ==> a
Must take care with overflow and exceptions e.g.: (i-j) + (i-j) ==> i+ i -j -j
Scalar Replacementof Aggregates
Scalar Replacement of Aggregates
Replace structures/arrays with collections of scalars
Expose the opportunity other further optimizations Especially register allocation
Exampletypedef struct{ int x; int y;}point;
void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}
int main ()
{
point p;
p.x = 1;
p.y = 2;
print (&p);
return 0;
}
Inliningtypedef struct{
int x;
int y;
}point;
void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}
int main ()
{
point p;
p.x = 1;
p.y = 2;
printf (“(%d, %d)”,
p.x, p.y);
return 0;
}
Replacementtypedef struct{
int x;
int y;
}point;
void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}
int main ()
{
int p_x;
int p_y;
p_x = 1;
p_y = 2;
printf (“(%d, %d)”,
p_x, p_y);
return 0;
}
Constant Propagationtypedef struct{
int x;
int y;
}point;
void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}
int main ()
{
int p_x;
int p_y;
p_x = 1;
p_y = 2;
printf (“(%d, %d)”,
1, 2);
return 0;
}
Flow-sensitive Optimizations
Constant propagation
a = x
x = 3…
…
Can we replace this “x” with constant “3”?
Do reaching definition analysis, if the definition “x=3” is the only definition of “x” that can reach the assignment “a=z”, then the “x” in “a=x” can be replaced by 3.
Copy propagation
a = x
x = y…
…
Can we replace this “x” with the variable “y”?
Do reaching definition analysis, if the definition “x=y” is the only definition of “x” that can reach the assignment “a=x”, then the “x” in the assignment “a=x” can be replaced by “y”.
Dead code elimination
return 0
x = v…
…Can we eliminate this assignment?
Do liveness analysis, one can eliminate the assignment “x=v”, if “x” does not live out of this assignment.
Must pay special attention toside effects. e.g.:
void main (){
x = 1/0;
}
Common Subexpression Elimination (CSE)
z = a+b
x = a+b…
…Use available expressions analysis to determine whether or not the expression “a+b” is available at definition site of “z”.
Use reaching expression analysis to locate calculation sites of “a+b”.
Can we replace the variable “z” with the variable “x”?
Common Subexpression Elimination (CSE)
z = t
t = a+b
x = tt = a+b
k = t h = a+b
t = ht is a fresh variable.
Similar code rewriting for other predecessor blocks.
Can we replace this “z” with “x”?