introduction to code optimization by dipankar

21
Introduction to Code Optimization Dipankar Nalui M.SC. Computer Science Dept. of Computer Science Pondicherry University

Upload: dipankar-nalui

Post on 24-Jun-2015

227 views

Category:

Education


1 download

DESCRIPTION

Compiler Design

TRANSCRIPT

Page 1: Introduction to code optimization by dipankar

Introduction to Code Optimization

Dipankar NaluiM.SC. Computer Science

Dept. of Computer SciencePondicherry University

Page 2: Introduction to code optimization by dipankar

Compiler

• A compiler is a program that accepts a high level language and produces a target language as output (Assembly Language).

CompilerAssembly Level

LanguageHigh Level Language

Page 3: Introduction to code optimization by dipankar

Phases of Compiler

1) Lexical Analysis2) Syntax Analysis3) Semantic Analysis4) Intermediate Code Generation

5)Code Optimization6) Code Generation

Page 4: Introduction to code optimization by dipankar

Code Optimization

•Optimization is the process of transforming a piece of code to make more efficient (either in terms of time or space) without changing its output or side-effects.

Page 5: Introduction to code optimization by dipankar

Why optimization is needed ???

• To improve intermediate code• Better target code• Executes Faster• Shorter code• Less power• Complexity : Time, Space & Cost• Efficient memory usage• Better performance.

Page 6: Introduction to code optimization by dipankar

“Optimization”

• The only difference visible to the code’s user should be that it runs faster and/or consumes less memory.• The name implies you are finding an "optimal“ solution— in truth,

optimization aims to improve, not perfect, the result.

Page 7: Introduction to code optimization by dipankar

Correctness Above All!

• Optimization should not change the correctness of the generated code.• Transforming the code to something that runs faster but incorrectly is

of little value.• It is expected that the un-optimized and optimized variants give the

same output for all inputs.

Page 8: Introduction to code optimization by dipankar

position = initial + rate * 60 id1 = id2 + id3 * 60

Intermediate CodeT1= int-to-float(60)T2=id3*T1T3=id2+T2id1=T3Eliminate int-to-float by replacing 60 by 60.0

Code Optimization

T1=id3 * 60.0Id1=id2 + t1Code Optimizer can deduce that the conversion of 60 from int to float can be done once.

Page 9: Introduction to code optimization by dipankar

The principal Sources of Optimization

• Some techniques are applied to the intermediate code, to streamline, rearrange, compress, etc.• Control-Flow Analysis• Local Optimizations• Constant Folding• Constant Propagation• Operator Strength Reduction• Copy Propagation• Dead Code Elimination• Common Subexpression Elimination• Global Optimizations, Data-Flow Analysis

Page 10: Introduction to code optimization by dipankar

Copy Propagation

Intermediate Code

tmp2 = tmp1 ;tmp3 = tmp2 * tmp1;tmp4 = tmp3 ;tmp5 = tmp3 * tmp2 ;c = tmp5 + tmp4 ;The code on the left makes a copy of tmp1 in tmp2 and a copy of tmp3 in tmp4.

Code Optimization

tmp3 = tmp1 * tmp1 ;tmp5 = tmp3 * tmp1 ;c = tmp5 + tmp3 ;we eliminated those unnecessary copies

Page 11: Introduction to code optimization by dipankar

Dead Code Elimination

• If an instruction’s result is never used, the instruction is considered "dead" and can be removed from the instruction stream.• tmp1 = tmp2 + tmp3 ;• if tmp1 is never used again, we can eliminate this instruction.• if tmp1 holds the result of a function call:• tmp1 = LCall _Binky;• Even if tmp1 is never used again, we cannot eliminate the instruction

because we can’t be sure that called function has no side-effects.

Page 12: Introduction to code optimization by dipankar

Common Sub-expression EliminationTwo operations are common if they produce the same result.

main(){int x, y, z;x = (1+20)* -x;y = x*x+(x/y);y = z = (x/y)/(x*x);}

tmp1 = 1 + 20 ;tmp2 = -x ;x = tmp1 * tmp2 ;tmp3 = x * x ;tmp4 = x / y ;y = tmp3 + tmp4 ;tmp5 = x / y ;tmp6 = x * x ;z = tmp5 / tmp6 ;y = z ;

Page 13: Introduction to code optimization by dipankar

Intermediate Code

tmp1 = 1 + 20 ;tmp2 = -x ;x = tmp1 * tmp2 ;tmp3 = x * x ;tmp4 = x / y ;y = tmp3 + tmp4 ;tmp5 = x / y ;tmp6 = x * x ;z = tmp5 / tmp6 ;y = z ;

Code Optimization

tmp2 = -x ;x = 21 * tmp2 ;tmp3 = x * x ;tmp4 = x / y ;y = tmp3 + tmp4 ;tmp5 = x / y ;z = tmp5 / tmp3 ;y = z ;

Page 14: Introduction to code optimization by dipankar

Algebraic Simplification

• x+0 = x• 0+x = x• x*1 = x• 1*x = x• 0/x = 0• x-0 = x• b && true = b• b && false = false• b || true = true• b || false = b

Page 15: Introduction to code optimization by dipankar

Example of Algebraic Simplification b = 5 + a + 10 ;

Intermediate Code_tmp0 = 5 ;_tmp1 = _tmp0 + a ;_tmp2 = _tmp1 + 10 ;b = _tmp2 ;

Code Optimization_tmp0 = 15 ;_tmp1 = a + _tmp0 ;b = _tmp1 ;

Page 16: Introduction to code optimization by dipankar

Constant Folding

• Evaluate constant expressions at compile time• Only possible when side-effect freeness guaranteed

c:= 1 + 3 c:= 4

true not false

Page 17: Introduction to code optimization by dipankar

Constant Propagation

• Variables that have constant value, e.g. c := 3• Later uses of c can be replaced by the constant• If no change of c between!

b := 3c := 1 + bd := b + c

b := 3c := 1 + 3d := 3 + c

Page 18: Introduction to code optimization by dipankar

Strength Reduction

• Replace expensive operations with simpler ones• Example: Multiplications replaced by additions

y := x * 2 y := x + x

Page 19: Introduction to code optimization by dipankar

Conclusion:

• Why do we optimize programs?• Is there an optimal optimizer?• Where in a compiler does optimization happen?• Does the optimization capture most of the potential improvement

without an unreasonable amount of effort ?• Does the optimization preserve the meaning of the source program ?• Does the optimization reduce the time and space?

Page 20: Introduction to code optimization by dipankar

Questions ??

Page 21: Introduction to code optimization by dipankar

Thank You …