170 likes | 319 Views
Optimizing Compilers. Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University. Content. Introduction Control flow analysis Data flow analysis Program transformations Case studies. Introduction. Optimizing Compilers.
E N D
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University
Content • Introduction • Control flow analysis • Data flow analysis • Program transformations • Case studies
Optimizing Compilers • Compilers that apply code-improving transformations • Must preserve the meaning of programs • Must improve programs by a measurable amount • Must be worth the effort
Levels of Code Optimization • Source code level • profile programs, change algorithms, transform loops • Intermediate code level • analyze programs, inline procedure calls, improve loops, eliminate common subexpressions • Target code level • use machine characteristics, do peephole optimizations
Organization of an Optimizing Compiler • Front end • Code optimization • control flow analysis • data flow analysis • code transformation • Code generation
Program Transformations • Statement level • common-subexpression elimination, copy propagation, dead-code elimination, constant folding, constant propagation • Loop level • code motion, reduction in strength, induction variable elimination • Procedure level • inline expansion, tail recursion optimization
Common Subexpression Elimination a = b + c b = a - d c = b + c d = a - d a = a + c c = a - d a = b + c b = a - d c = b + c d = b a = a + c c = a - d
Copy Propagation a = b + c b = a - d c = b + c d = b a = a + c c = a - d a = b + c b = a - d c = b + c d = b a = a + c c = a - b
Dead-Code Elimination a = b + c b = a - d c = b + c d = b a = a + c c = a - b a = b + c b = a - d c = b + c a = a + c c = a - b
Constant Folding a = 4 * 2 b = a + 3 a = 8 b = a + 3
Constant Propagation a = 8 b = a + 3 a = 8 b = 8 + 3
Code Motion i = 0 f = n - 1 i = 0 L: t = 4 * i b = a[t] i = i + 1 if i < n- 1goto L L: t = 4 * i b = a[t] i = i + 1 if i < fgoto L
Reduction in Strength i = 0 f = n - 1 t = -4 i = 0 f = n - 1 t = 4 * i b = a[t] i = i + 1 if i < f goto L t = t + 4 b = a[t] i = i + 1 if i < f goto L
Induction Variable Elimination i = 0 f = n - 1 t = -4 i = 0 f = n - 1 t = -4 t = t + 4 b = a[t] ift < 4 * fgoto L t = t + 4 b = a[t] i = i + 1 ifi < fgoto L
Inline Expansion p: a = c + d b = a + c param a param b call q, 2 c = d + e return q: d = sp[0] + sp[1] return p: a = c + d b = a + c d = a + b c = d + e return q: d = sp[0] + sp[1] return
Tail Recursion Optimization p: a = sp[0] + c b = sp[1] + d param a param b call p, 2 return p: a = sp[0] + c b = sp[1] + d sp[0] = a sp[1] = b goto p return