Code Optimization
Compiler Code Optimizations
Introduction
• Optimized code
Executes faster
efficient memory usage
yielding better performance.
• Compilers can be designed to provide code
optimization.
• Users should only focus on optimizations not provided
by the compiler such as choosing a faster and/or less
memory intensive algorithm.
Topics
Machine-independent Understanding processor
optimizations optimization
Code motion
Reduction in strength
Translation of instructions
into operations
Common subexpression
sharing Out-of-order execution
Tuning: Identifying
Branches
performance bottlenecks
Machine-dependent Caches and Blocking
optimizations
Pointer code Advice
Loop unrolling
Enabling instruction-level
parallelism
Speed and optimization
Programmer Processor
Choice of algorithm
Pipelining
Intelligent coding
Multiple execution units
Compiler Memory accesses
Choice of instructions Branches
Moving code Caches
Reordering code
Strength reduction
Must be faithful to original Rest of system
program
Uncontrollable
Optimizing Compilers
Provide efficient mapping of program to machine
Register allocation
Code selection and ordering
Eliminating minor inefficiencies
Don’t (usually) improve asymptotic efficiency
Up to programmer to select best overall algorithm
Big-O savings are (often) more important than
constant factors
But constant factors also matter
Have difficulty overcoming “optimization blockers”
Potential memory aliasing
Potential procedure side effects
Basic Block
• BB is a sequence of consecutive statements in
which the flow control enters at the beginning and
leaves at the end w/o halt or possible branching
except at the end
Basic Block
Principle sources of optimization
• Local optimization: within a basic block
• Global optimization: otherwise
• Mixed
Function-Preserving Transformation
• Improving performance w/o changing fn.
• Techniques
– Common subexpression Elimination
– Copy Propagation
– Dead-code elimination
– Constant folding
Common subexpression
Elimination
• An occurrence of an expression E is common
subexpression if E was previously computed and the
values of variables in E have not changed since.
Copy Propagation
• An idea behind this technique is to use g for f
whenever possible after the copy of
f := g
before After
x := t3 x := t3
a[t7] := t5 a[t7] := t5
a[t10] := x a[t10] := t3
Goto b2 Goto b2
Dead code elimination
• Remove unreachable code
• If (debug) print …
• Many times, debug := false
Loop optimizations
• Beyond basic block
• Three important techniques
– Code motion
– Induction-variable elimination
– Reduction in strength
Code motion
• Move code outside the loop since there are potential
many iterations
• Look for expressions that yeild the same result
independent of the iterations.
• before
• While ( I <= limit – 2).
after
•
T = limit – 2
While ( I <= t)
Induction-variable elimination &
Reduction in strength
• Look for induction variables for strength reductions
• E.g. a pattern of changes in a lock step
B3: B3:
j = j - 1 j = j - 1
t4 = 4 *j t4 = t4 -4
t5 = a [ t4] t5 = a [ t4]
If t5 > v goto B3 If t5 > v goto B3
Others optimizations
• Optimizations for Basic blocks
• Reducible flow graph
• Global Data flow analysis
• Machine dependent Optimizations
Basic Blocks
DAG