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

PDF
Optimization in Programming languages
PPT
Code Tuning
PPT
basics of optimizations presentation s
PPT
u5 code optimization and code generation.ppt
PPTX
Code optimization
PDF
Code Optimizatoion
PPTX
lab-8 (1).pptx
Optimization in Programming languages
Code Tuning
basics of optimizations presentation s
u5 code optimization and code generation.ppt
Code optimization
Code Optimizatoion
lab-8 (1).pptx

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

PPTX
Repair dagstuhl jan2017
PPTX
Topic 2_revised.pptx
PPTX
How to add an optimization for C# to RyuJIT
PPT
4.Support Vector Machines.ppt machine learning and development
PPSX
White Box testing by Pankaj Thakur, NITTTR Chandigarh
PPT
C language programming
PPTX
Dzanan_Bajgoric_C2CUDA_MscThesis_Present
PPT
CS 354 More Graphics Pipeline
PPT
PERFORMANCE EVALUATION PARAMETERS FOR MACHINE LEARNING
PPTX
Object oriented programming system with C++
PPT
EMBEDDED SYSTEMS 4&5
PPTX
Bit-Manipulation for competitive programming
PDF
Acm aleppo cpc training introduction 1
PPT
C language programming
PPTX
20101017 program analysis_for_security_livshits_lecture02_compilers
PDF
openMP loop parallelization
PDF
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
PPTX
Enhancing the performance of kmeans algorithm
PDF
Fffgggggfffffffggggggggggggggggggile.pdf
PDF
Lesson 24. Phantom errors
Repair dagstuhl jan2017
Topic 2_revised.pptx
How to add an optimization for C# to RyuJIT
4.Support Vector Machines.ppt machine learning and development
White Box testing by Pankaj Thakur, NITTTR Chandigarh
C language programming
Dzanan_Bajgoric_C2CUDA_MscThesis_Present
CS 354 More Graphics Pipeline
PERFORMANCE EVALUATION PARAMETERS FOR MACHINE LEARNING
Object oriented programming system with C++
EMBEDDED SYSTEMS 4&5
Bit-Manipulation for competitive programming
Acm aleppo cpc training introduction 1
C language programming
20101017 program analysis_for_security_livshits_lecture02_compilers
openMP loop parallelization
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
Enhancing the performance of kmeans algorithm
Fffgggggfffffffggggggggggggggggggile.pdf
Lesson 24. Phantom errors
Ad

Recently uploaded (20)

PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Construction Project Organization Group 2.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Project quality management in manufacturing
PPTX
Geodesy 1.pptx...............................................
DOCX
573137875-Attendance-Management-System-original
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Structs to JSON How Go Powers REST APIs.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Lesson 3_Tessellation.pptx finite Mathematics
Construction Project Organization Group 2.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Project quality management in manufacturing
Geodesy 1.pptx...............................................
573137875-Attendance-Management-System-original
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Digital Logic Computer Design lecture notes
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
Model Code of Practice - Construction Work - 21102022 .pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
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