Download presentation
Presentation is loading. Please wait.
Published byCaroline Edwards Modified over 9 years ago
1
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University
2
Content Introduction Control flow analysis Data flow analysis Program transformations Case studies
3
Introduction
4
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
5
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
6
Organization of an Optimizing Compiler Front end Code optimization –control flow analysis –data flow analysis –code transformation Code generation
7
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
8
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
9
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
10
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
11
Constant Folding a = 4 * 2 b = a + 3 a = 8 b = a + 3
12
Constant Propagation a = 8 b = a + 3 a = 8 b = 8 + 3
13
Code Motion t = 4 * i b = a[t] i = i + 1 if i < n- 1 goto L i = 0 L: t = 4 * i b = a[t] i = i + 1 if i < f goto L i = 0 f = n - 1 L:
14
Reduction in Strength t = 4 * i b = a[t] i = i + 1 if i < f goto L i = 0 f = n - 1 t = t + 4 b = a[t] i = i + 1 if i < f goto L i = 0 f = n - 1 t = -4
15
Induction Variable Elimination t = t + 4 b = a[t] if t < 4 * f goto L i = 0 f = n - 1 t = -4 t = t + 4 b = a[t] i = i + 1 if i < f goto L i = 0 f = n - 1 t = -4
16
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
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.