SlideShare a Scribd company logo
Code Optimization
Overview and Examples
Code Optimization
 Why
 Reduce programmers’ burden
 Allow programmers to concentrate on high level concept
 Without worrying about performance issues
 Target
 Reduce execution time
 Reduce space
 Sometimes, these are tradeoffs
.
Code Optimization
 Scope
 Peephole analysis
 Within one or a few instructions
 Local analysis
 Within a basic block
 Global analysis
 Entire procedure or within a certain scope
Code Optimization
 Techniques
 Constant propagation
 Algebraic simplification, strength reduction
 Copy propagation
 Common subexpression elimination
 Unreachable code elimination
 Dead code elimination
 Loop Optimization
Code Optimization Techniques
 Constant propagation
 If the value of a variable is a constant, then replace the variable by the
constant
 It is not the constant definition, but a variable is assigned to a constant
 The variable may not always be a constant
 E.g.
N := 10; C := 2;
for (i:=0; i<N; i++) {s = s + i*C; }
 for (i:=0; i<10; i++) { s = s + i*2; }
If (C) go to …  go to …
 The other branch, if any, can be eliminated by other optimizations
 Requirement:
 After a constant assignment to the variable
 Until next assignment of the variable
 Perform data flow analysis to determine the propagation
Code Optimization Techniques
 Algebraic simplification
 More general form of constant folding, e.g.,
 x + 0  x x – 0  x
 x * 1  x x / 1  x
 x * 0  0
 Repeatedly apply the rules
 (y * 1 + 0) / 1  y
 Strength reduction
 Replace expensive operations
 E.g., x : = 2*2*2*2
  x : = 8+8
Code Optimization Techniques
 Copy propagation
 Extension of constant propagation
 After y is assigned to x, use y to replace x till x is assigned again
 Example
x := y;
 s := y * f(y)
s := x * f(x)
 Reduce the copying
 If y is reassigned in between, then this action cannot be performed
Code Optimization Techniques
 Common subexpression elimination
 Example:
a := b + c a := b + c
c := b + c  c := a
d := b + c d := a
 Example in array index calculations
 c[i+1] := a[i+1] + b[i+1]
 During address computation, i+1 should be reused
 Not visible in high level code, but in intermediate code
Code Optimization Techniques
 Unreacheable code elimination
 Construct the control flow graph
 Unreachable code block will not have an incoming edge.
 After constant propagation/folding, unreachable branches can be
eliminated.
 Dead code elimination
 Ineffective statements
 x := y + 1 (immediately redefined, eliminate!)
 y := 5  y := 5
 x := 2 * z x := 2 * z
 A variable is dead if it is never used after last definition
 Eliminate assignments to dead variables
 Need to do data flow analysis to find dead variables
Code Optimization Techniques
 Loop optimization
 Consumes 90% of the execution time
 a larger payoff to optimize the code within a loop
 Techniques
 Loop invariant detection and code motion
 Induction variable elimination
 Strength reduction in loops
 Loop unrolling
 Loop fusion
Code Optimization Techniques
 Loop invariant detection and code motion
 If the result of a statement or expression does not change within a
loop, and it has no external side-effect
 Computation can be moved to outside of the loop
 Example
for (i=0; i<n; i++) {a[i] := a[i] + x/y;}
 Three address code
 c := x/y;
for (i=0; i<n; i++)
{a[i] := a[i] + c;}
Code Optimization Techniques
 Strength reduction in loops
 Example
s :=v:= 0;
for (i=1; i<n; i++)
{ v := 4 * i;
s := s + v; ) 4,4 8,12 12, 24 16,40
s := v:= 0;
 for (i=1; i<n; i++)
 { v := v + 4; s := s + v; ) 4,4 8,12 12,24
 Induction variable elimination
 If there are multiple induction variables in a loop, can eliminate
the ones which are used only in the test condition
 Example
s := 0; for (i=1; i<=n; i++) { s := 4 * i; … } 4,8,12,16
 s := 0; e := 4*n; while (s < e) { s := s + 4; } 4,8,12,16
Code Optimization Techniques
 Loop unrolling
 Execute loop body multiple times at each iteration
 Get rid of the conditional branches, if possible
 Allow optimization to cross multiple iterations of the loop
 Especially for parallel instruction execution.
For(i=0; i<100; i++) 200 sec
{
Cout <<A[i]
}
For(i=0; i<100; i=i+2) 150sec
{
Cout <<A[i]
Cout <<A[i+1];
}
Code Optimization Techniques
 Loop fusion
 Example
for i=1 to N do 600sec
A[i] = B[i] + 1
endfor
for i=1 to N do
C[i] = A[i] / 2
endfor
for i=1 to N do
D[i] = 1 / C[i+1]
endfor
Before Loop Fusion
for i=1 to N do 100+300=400
A[i] = B[i] + 1
C[i] = A[i] / 2
D[i] = 1 / C[i+1]
endfor
Is this correct?
Actually, cannot fuse
the third loop

More Related Content

Similar to Code Optimization Lec#7.ppt Code Optimizer (20)

PPTX
Download Artweaver Plus Cracked Version Free
adomalex314
 
PPTX
Latest TreeSize Professional 9 Crack Download
bentenkhan340
 
PPTX
PowerDirector Activated Full Tested Download
tomveren
 
PPTX
Code optimization
veena venugopal
 
PPTX
Code optimization
veena venugopal
 
PPTX
Code Optimization
Akhil Kaushik
 
PPTX
Principal Sources of Optimization in compiler design
LogsAk
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PPTX
Compiler optimization techniques
Hardik Devani
 
PPTX
Code optimization
Pradip Bhattarai
 
PPTX
sCode optimization
Satyamevjayte Haxor
 
PPTX
Code optimization
Sameenafathima4
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
PPT
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
PPTX
Introduction to code optimization by dipankar
Dipankar Nalui
 
PPT
lect23_optimization.ppt
ssuser0be977
 
PPTX
Principal source of optimization in compiler design
Rajkumar R
 
Download Artweaver Plus Cracked Version Free
adomalex314
 
Latest TreeSize Professional 9 Crack Download
bentenkhan340
 
PowerDirector Activated Full Tested Download
tomveren
 
Code optimization
veena venugopal
 
Code optimization
veena venugopal
 
Code Optimization
Akhil Kaushik
 
Principal Sources of Optimization in compiler design
LogsAk
 
complier design unit 5 for helping students
aniketsugandhi1
 
Compiler optimization techniques
Hardik Devani
 
Code optimization
Pradip Bhattarai
 
sCode optimization
Satyamevjayte Haxor
 
Code optimization
Sameenafathima4
 
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
vtu21524
 
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
Introduction to code optimization by dipankar
Dipankar Nalui
 
lect23_optimization.ppt
ssuser0be977
 
Principal source of optimization in compiler design
Rajkumar R
 

Recently uploaded (20)

PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Digital water marking system project report
Kamal Acharya
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Ad

Code Optimization Lec#7.ppt Code Optimizer

  • 2. Code Optimization  Why  Reduce programmers’ burden  Allow programmers to concentrate on high level concept  Without worrying about performance issues  Target  Reduce execution time  Reduce space  Sometimes, these are tradeoffs .
  • 3. Code Optimization  Scope  Peephole analysis  Within one or a few instructions  Local analysis  Within a basic block  Global analysis  Entire procedure or within a certain scope
  • 4. Code Optimization  Techniques  Constant propagation  Algebraic simplification, strength reduction  Copy propagation  Common subexpression elimination  Unreachable code elimination  Dead code elimination  Loop Optimization
  • 5. Code Optimization Techniques  Constant propagation  If the value of a variable is a constant, then replace the variable by the constant  It is not the constant definition, but a variable is assigned to a constant  The variable may not always be a constant  E.g. N := 10; C := 2; for (i:=0; i<N; i++) {s = s + i*C; }  for (i:=0; i<10; i++) { s = s + i*2; } If (C) go to …  go to …  The other branch, if any, can be eliminated by other optimizations  Requirement:  After a constant assignment to the variable  Until next assignment of the variable  Perform data flow analysis to determine the propagation
  • 6. Code Optimization Techniques  Algebraic simplification  More general form of constant folding, e.g.,  x + 0  x x – 0  x  x * 1  x x / 1  x  x * 0  0  Repeatedly apply the rules  (y * 1 + 0) / 1  y  Strength reduction  Replace expensive operations  E.g., x : = 2*2*2*2   x : = 8+8
  • 7. Code Optimization Techniques  Copy propagation  Extension of constant propagation  After y is assigned to x, use y to replace x till x is assigned again  Example x := y;  s := y * f(y) s := x * f(x)  Reduce the copying  If y is reassigned in between, then this action cannot be performed
  • 8. Code Optimization Techniques  Common subexpression elimination  Example: a := b + c a := b + c c := b + c  c := a d := b + c d := a  Example in array index calculations  c[i+1] := a[i+1] + b[i+1]  During address computation, i+1 should be reused  Not visible in high level code, but in intermediate code
  • 9. Code Optimization Techniques  Unreacheable code elimination  Construct the control flow graph  Unreachable code block will not have an incoming edge.  After constant propagation/folding, unreachable branches can be eliminated.  Dead code elimination  Ineffective statements  x := y + 1 (immediately redefined, eliminate!)  y := 5  y := 5  x := 2 * z x := 2 * z  A variable is dead if it is never used after last definition  Eliminate assignments to dead variables  Need to do data flow analysis to find dead variables
  • 10. Code Optimization Techniques  Loop optimization  Consumes 90% of the execution time  a larger payoff to optimize the code within a loop  Techniques  Loop invariant detection and code motion  Induction variable elimination  Strength reduction in loops  Loop unrolling  Loop fusion
  • 11. Code Optimization Techniques  Loop invariant detection and code motion  If the result of a statement or expression does not change within a loop, and it has no external side-effect  Computation can be moved to outside of the loop  Example for (i=0; i<n; i++) {a[i] := a[i] + x/y;}  Three address code  c := x/y; for (i=0; i<n; i++) {a[i] := a[i] + c;}
  • 12. Code Optimization Techniques  Strength reduction in loops  Example s :=v:= 0; for (i=1; i<n; i++) { v := 4 * i; s := s + v; ) 4,4 8,12 12, 24 16,40 s := v:= 0;  for (i=1; i<n; i++)  { v := v + 4; s := s + v; ) 4,4 8,12 12,24  Induction variable elimination  If there are multiple induction variables in a loop, can eliminate the ones which are used only in the test condition  Example s := 0; for (i=1; i<=n; i++) { s := 4 * i; … } 4,8,12,16  s := 0; e := 4*n; while (s < e) { s := s + 4; } 4,8,12,16
  • 13. Code Optimization Techniques  Loop unrolling  Execute loop body multiple times at each iteration  Get rid of the conditional branches, if possible  Allow optimization to cross multiple iterations of the loop  Especially for parallel instruction execution. For(i=0; i<100; i++) 200 sec { Cout <<A[i] } For(i=0; i<100; i=i+2) 150sec { Cout <<A[i] Cout <<A[i+1]; }
  • 14. Code Optimization Techniques  Loop fusion  Example for i=1 to N do 600sec A[i] = B[i] + 1 endfor for i=1 to N do C[i] = A[i] / 2 endfor for i=1 to N do D[i] = 1 / C[i+1] endfor Before Loop Fusion for i=1 to N do 100+300=400 A[i] = B[i] + 1 C[i] = A[i] / 2 D[i] = 1 / C[i+1] endfor Is this correct? Actually, cannot fuse the third loop