KEMBAR78
Code Optimization Lec#7.ppt Code Optimizer | PPT
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

Code Optimization Lec#7.ppt Code Optimizer

  • 1.
  • 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