Module 12: View
  Lecture 24: Code Optimization
 


Renaming Temporary Variable
Rename temporary variable t to u and replace all the occurrences of
t by u.
This transformation increases parallelism.
Interchange Statements Two statements may be interchanged if value of the block is not affected.

t1 = b + c
t2 = X + Y
t2 = X + Y
t1 = b + c

Constant folding Evaluate constant expressions at compile time.

X = 3 + 5
Y = X * 2
X = 8
Y = 16

Algebraic Transformation

  • Eliminate addition/subtraction with 0 X = X ± 0 should be eliminated.
  • Eliminate multiplication/division by 1 X = X ∗ 1 or X = X/1 should be eliminated.
  • Eliminate multiplication by 0 X = X * 0 should be replaced with X = 0

Strength reduction Costly operators should be replaced by cheaper operators

  • Replace T = X * * ∗2 by T = X ∗ X
  • Replace T = X * 4 by T = ls(X, 2)
  • Replace 2 * X by X + X
  • Replace X * 0.5 by X/2
  • Replace X/2 by rs(X, 1)

Loop Optimizations

Code Motion: Expression not evaluated in the code must be moved out of loop

while( i <= limit - 2 ) {
\* statement not changing limit *\
}
t := limit - 2
while( i <= t ) { statement }