Download Code Optimization: Techniques and Phases - Prof. Stephen J. Allan and more Study notes Computer Science in PDF only on Docsity!
Code Optimization
CS 5300 - SJAllan Code Optimization 2
Code Optimization and Phases
Code Optimizer
Data- Flow Analysis
Control- Flow Analysis
Transformations
Front End
Code Generator
CS 5300 - SJAllan Code Optimization 3
Optimization
Code produced by standard algorithms can often be made
to run faster, take less space or both
These improvements are achieved through transformations
called optimizations
Compilers that apply these transformations are called
optimizing compilers
It is especially important to optimize frequently executed
parts of a program
- The 90/10 rule
- Profiling can be helpful, but compilers do not typically have sample
input data
- Inner loops tend to be good candidates for optimization
CS 5300 - SJAllan Code Optimization 4
Criteria for Transformations
A transformation must preserve the meaning
of a program
– Cannot change the output produced for any
input
– Can not introduce an error
Transformations should, on average, speed
up programs
Transformations should be worth the effort
CS 5300 - SJAllan Code Optimization 7
Redundant-Instruction Elimination
Redundant loads and stores:
1. lw $t0, a
2. sw $t0, a
Unreachable code:
#define debug 0
if (debug) {
/* print debugging information */
CS 5300 - SJAllan Code Optimization 8
Flow-of-Control Optimizations
Jumps to jumps, jumps to conditional jumps, and
conditional jumps to jumps are not necessary
Jumps to jumps example:
- The following replacement is valid:
- If there are no other jumps to L1 and L1 is preceded by an
unconditional jump, the statement at L1 can be eliminated
Jumps to conditional jumps and conditional jumps to jumps
lead to similar transformations
goto L
… L1: goto L
goto L
… L1: goto L
CS 5300 - SJAllan Code Optimization 9
Other Peephole Optimizations
A few algebraic identities that occur frequently (such as x
:= x + 0 or x := x * 1) can be eliminated
Reduction in strength replaces expensive operations with
cheaper ones
- Calculating x * x is likely much cheaper than x^2 using an
exponentiation routine
- It may be cheaper to implement x * 5 as x * 4 + x
Some machines may have hardware instructions to
implement certain specific operations efficiently
- For example, auto-increment may be cheaper than a straight-
forward x := x + 1
- Auto-increment and auto-decrement are also useful when pushing
into or popping off of a stack
CS 5300 - SJAllan Code Optimization 10
Optimizing Intermediate Code
This phase is generally only included in optimizing
compilers
Offers the following advantages:
- Operations needed to implement high-level constructs
are made explicit (i.e., address calculations)
- Intermediate code is independent of target machine;
code generator can be replaced for different machine
We are assuming intermediate code uses three-
address instructions
CS 5300 - SJAllan Code Optimization 13
Control Flow Graph
Nodes represent basic blocks
The initial node is the block whose leader is the
first statement
There exists an edge from Bi to Bj if:
- There is a conditional or unconditional jump from the
last statement in Bi to the first statement in Bj
- Bj immediately follows Bi in the order of the program,
and Bi does not end in an unconditional jump
CS 5300 - SJAllan Code Optimization 14
Flow Graph
CS 5300 - SJAllan Code Optimization 15
Local vs. Global Transformations
Local transformations involve statements
within a single basic block
All other transformations are called global
transformations
Local transformations are generally
performed first
Many types of transformations can be
performed either locally or globally
CS 5300 - SJAllan Code Optimization 16
Common Subexpressions
E is a common subexpression if:
– E was previously computed
– Variables in E have not changed since previous
computation
Can avoid recomputing E if previously
computed value is still available
Dags are useful to detect common
subexpressions
CS 5300 - SJAllan Code Optimization 19
Copy Propagation
Assignments of the form f := g are called copy
statements (or copies)
The idea behind copy propagation is to use g for f
whenever possible after such a statement
For example, applied to block B5 of the previous
flow graph, we obtain:
x := t
a[t2] := t
a[t4] := t
goto B
Copy propagation often turns the copy statement
into "dead code"
CS 5300 - SJAllan Code Optimization 20
Dead-Code Elimination
Dead code includes code that can never be reached and
code that computes a value that never gets used
Consider: if (debug) print …
- It can sometimes be deduced at compile time that the value of an
expression is constant
- Then the constant can be used in place of the expression (constant
folding)
- Let's assume a previous statement assigns debug := false and
value never changes
- Then the print statement becomes unreachable and can be
eliminated
Consider the example from the previous slide
- The value of x computed by the copy statement never gets used
after the copy propagation
- The copy statement is now dead code and can be eliminated
CS 5300 - SJAllan Code Optimization 21
Loop Optimizations
The running time of a program may be improved if
we do both of the following:
- Decrease the number of statements in an inner loop
- Increase the number of statements in the outer loop
Code motion moves code outside of a loop
while (i <= limit - 2) …
- The result of code motion would be:
t = limit – 2
while (i <= t) …
CS 5300 - SJAllan Code Optimization 22
Loop Optimizations
Induction variables: variables that remain in "lock-step"
- For example, in block B3 of previous flow graph, j and t4 are
induction variables
- Induction-variable elimination can sometimes eliminate all but one
of a set of induction variables
Reduction in strength replaces a more expensive operation
with a less expensive one
- For example, in block B3, t4 decreases by four with every iteration
- If initialized correctly, can replace multiplication with subtraction
- Often application of reduction in strength leads to induction-variable
elimination
Methods exist to recognize induction variables and apply
appropriate transformations automatically
CS 5300 - SJAllan Code Optimization 25
Dominators
SCRs are found by using dominators
Node d of a CFG dominates node n if every
path from the initial node of the CFG to n
goes through d
Every node dominates itself and the entry
node dominates all nodes in the loop
CS 5300 - SJAllan Code Optimization 26
Dominator Example
4 all but 1, 2, & 3
3 all but 1 & 2
1 everything
Node Dominates
CS 5300 - SJAllan Code Optimization 27
Dominator Algorithm
D(n 0 ) = {n 0 } for n ∈ N – {n 0 } D(n) = N end for Change = TRUE while ( Change ) Change = FALSE for n ∈ N – {n 0 } NewDom = {n} ∪ ( ∩ D(p) for all pred(n) ) if ( D(n) ≠ NewDom ) then Change = TRUE end if D(n) = NewDom end for end while
CS 5300 - SJAllan Code Optimization 28
Natural Loops
Back edge
– Edge whose head dominates its tail
The natural loop formed from the back edge
n → d can be identified by finding those
nodes that can reach n without going
through d
– These nodes, plus d , form the natural loop
CS 5300 - SJAllan Code Optimization 31
Natural Loops
Back Edge Natural Loops
CS 5300 - SJAllan Code Optimization 32
Another Example
D(1) = {1}
D(2) = {1,2}
D(3) = {1,2,3}
D(4) = {1,2,3,4}
D(5) = {1,2,3,4,5}
D(6) = {1,2,3,4,6}
D(7) = {1,2,7}
Dominators
Natural Loops
CS 5300 - SJAllan Code Optimization 33
More on Natural Loops
Natural loops have a useful property
– If two loops have the same header, they are
either:
Disjoint or
One is entirely contained (nested within) the other
Thus neglecting loops with the same header
we have the notion of an inner loop
– One that contains no other loop
CS 5300 - SJAllan Code Optimization 34
Transformations on Loops
Several transformations require us to move
statements “before the header”
- Thus we begin treatment of a loop L by creating a new
block, called the preheader
Only the header as a successor
All edges that formally entered the header of L from outside L,
instead enter the preheader
- Edges inside the loop are not changed
- Initially the preheader is empty but transformation will
place statements in it
CS 5300 - SJAllan Code Optimization 37
Data Flow Analysis
We assume the following:
– All relevant local data flow information is
available for a particular procedure
– Any variable aliasing is known
– The CFG for the procedure is given
Information can be associated with the top
or the bottom of a node
The sets of information can be conveniently
represented by bit vectors
CS 5300 - SJAllan Code Optimization 38
Reaching Definitions
The problem is to determine the sets
RDTOP( x ) and RDBOT( x ) of variable
definitions that can “reach” the top and
bottom of each node x in the CFB
A definition-clear path with respect to a
variable v means there is no definition of v
on that path
CS 5300 - SJAllan Code Optimization 39
Reaching Definitions
A definition d of a variable v in a node x is said to reach the
top (bottom) of node y iff d occurs in node x and there is a
definition-clear path for v from d to the top (bottom) of node
y
The following information has to be available at the bottom
of each node x :
- XDEFS( x ) – set of locally exposed definitions of node x A locally exposed definition is the last definition of a variable in the node
- PRESERVED( x ) – set of definitions preserved by node x A definition d of a variable v is said to kill all definitions of the same variable that reach d. Any definition of a variable v that reaches the top of a node x , and there is no definition of variable v in x is said to be preserved by x.
CS 5300 - SJAllan Code Optimization 40
Equations for Reaching Definitions
RDTOP(x) = ∪ ( RDBOT(p) ∀ p ∈ pred(x) )
RDBOT(x) = ( RDTOP(x) ∩ PRESERVED(x) ) ∪
XDEFS(x)
If pred(s) = φ, where s is the start node, then
RDTOP(x) = φ