Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Code Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Code Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Outline

Compiler Optimization

– Local/peephole optimizations

– Other sources of optimization

– Recognizing loops

– Dataflow analysis
Outline (cont’d)

Optimizing (cont’d)

– Algorithms for global optimizations

– Alias analysis

Code generation

– Instruction selection

– Register allocation

– Instruction scheduling: improving ILP

Optimizations for cache behavior


Intermediate Representation (IR)

Flow graph

– Nodes are basic blocks


Basic blocks are single entry and single exit

– Edges represent control-flow

Abstract Machine Code

– Including the notion of functions and procedures

Symbol table(s) keep track of scope and binding information about


names
Partitioning into basic blocks

1. Determine the leaders, which are:

The first statement

Any statement that is the target of a jump

Any statement that immediately follows a jump

2. For each leader its basic block consists of the leader and all
statements up to but not including the next leader
Partitioning into basic blocks (cont’d)

1 prod=0
BB1
2 i=1
3 t1=4*i
4 t2=a[t1]
5 t3=4*i
6 t4=b[t3]
BB2 7 t5=t2*t4
8 t6=prod+t5
9 prod=t6
10 t7=i+i
11 i=t7
12 if i < 21 goto 3
Intermediate Representation (cont’d)

Structure within a basic block:

Abstract Syntax Tree (AST)

– Leaves are labeled by variable names or constants

– Interior nodes are labeled by an operator

Directed Acyclic Graph (DAG)

C-like

3 address statements (like we have already seen)


Directed Acyclic Graph

Like ASTs:

– Leaves are labeled by variable names or constants

– Interior nodes are labeled by an operator

Nodes can have variable names attached that contain the value of
that expression

Common subexpressions are represented by multiple edges to the


same expression
DAG creation

Suppose the following three address statements:

1. op





2. op



3.








... will be treated like case 1 with undefined





DAG example

+ t6, prod
1 t1 = 4 * i
2 t2 = a[t1] prod * t5
3 t3 = 4 * i
[] t2 [] t4 <=
4 t4 = b[t3]
5 t5 = t2 * t4 t1, t3 + t7, i
6 t6 = prod + t5 * 20

7 prod = t6
a b 4 i 1
8 t7 = i + 1
9 i = t7



10 if ( ) goto 1



Local optimizations

On basic blocks in the intermediate representation

– Machine independent optimizations

As a post code-generation step (often called peephole optimization)

– On a small “instruction window” (often a basic block)

– Includes machine specific optimizations


Transformations on basic blocks

Examples

Function-preserving transformations

– Common subexpression elimination

– Constant folding

– Copy propagation

– Dead-code elimination

– Temporary variable renaming

– Interchange of independent statements


Transformations on basic blocks (cont’d)

Algebraic transformations

Machine dependent eliminations/transformations

– Removal of redundant loads/stores

– Use of machine idioms


Common subexpression elimination

If the same expression is computed more than once it is called a


common subexpression

If the result of the expression is stored, we don’t have to recompute it

Moving to a DAG as IR, common subexpressions are automatically


detected!





















Constant folding

Compute constant expression at compile time

May require some emulation support
















Copy propagation

Propagate original values when copied

Target for dead-code elimination















Dead-code elimination

A variable is dead at a statement if it is not used after that


statement

An assignment where is dead can be safely eliminated








Requires live-variable analysis (discussed later on)


Temporary variable renaming













































If each statement that defines a temporary defines a new temporary,
then the basic block is in normal-form

– Makes some optimizations at BB level a lot simpler (e.g. common


subexpression elimination, copy propagation, etc.)
Algebraic transformations

There are many possible algebraic transformations

Usually only the common ones are implemented




















Machine dependent eliminations/transformations

Removal of redundant loads/stores


1 mov R0, a
2 mov a, R0 // can be removed
Removal of redundant jumps, for example
1 beq ...,$Lx bne ...,$Ly
2 j $Ly $Lx: ...


3 $Lx: ...
Use of machine idioms, e.g.,

– Auto increment/decrement addressing modes

– SIMD instructions

Etc., etc. (see practical assignment)


Other sources of optimizations

Global optimizations

– Global common subexpression elimination

– Global constant folding

– Global copy propagation, etc.

Loop optimizations

They all need some dataflow analysis on the flow graph


Loop optimizations

Code motion

Decrease amount of code inside loop

Take a loop-invariant expression and place it before the loop





while ( )













while ( )


Loop optimizations (cont’d)

Induction variable elimination

Variables that are locked to the iteration of the loop are called
induction variables


Example: in for (i = 0; i < 10; i++) is an induction
variable

Loops can contain more than one induction variable, for example,
hidden in an array lookup computation

Often, we can eliminate these extra induction variables


Loop optimizations (cont’d)

Strength reduction

Strength reduction is the replacement of expensive operations by


cheaper ones (algebraic transformation)

Its use is not limited to loops but can be helpful for induction variable
elimination




































if ( ) goto top if ( ) goto top





Loop optimizations (cont’d)

Induction variable elimination (2)


Note that in the previous strength reduction we have to initialize
before the loop

After such strength reductions we can eliminate an induction variable


























if ( ) goto top







if ( ) goto top

Algorithms for global optimizations

Global common subexpression elimination

First calculate the sets of available expressions

For every statement of the form where is






available do the following

– Search backwards in the graph for the evaluations of





– Create a new variable

– Replace statements by










– Replace statement by



Copy propagation

Suppose a copy statement of the form is encountered. We



may now substitute a use of by a use of if


– Statement is the only definition of reaching the use


– On every path from statement to the use, there are no


assignments to

Copy propagation (cont’d)
To find the set of copy statements we can use, we define a new
dataflow problem

An occurrence of a copy statement generates this statement

An assignment to or kills the copy statement



Dataflow equations:






















for B not initial





















where B1 is the initial block



Copy propagation (cont’d)

For each copy statement : do



– Determine the uses of reached by this definition of

– Determine if for each of those uses this is the only definition







reaching it ( )





– If so, remove and replace the uses of by uses of





Detection of loop-invariant computations

1. Mark invariant those statements whose operands are constant or


have reaching definitions outside the loop

2. Repeat step 3 until no new statements are marked invariant

3. Mark invariant those statements whose operands either are


constant, have reaching definitions outside the loop, or have one
reaching definition that is marked invariant
Code motion

1. Create a pre-header for the loop

2. Find loop-invariant statements

3. For each statement defining found in step 2, check that


(a) it is in a block that dominate all exits of the loop

(b) is not defined elsewhere in the loop

(c) all uses of in the loop can only be reached from


this statement


4. Move the statements that conform to the pre-header


Code motion (cont’d)

i = 1 B1
i = 1 B1 i = 1 B1
i=3
if u < v goto B3 B2 if u < v goto B3 B2 if u < v goto B3 B2

i=2 i=2 i=2


B3 B3 B3
u=u+1 u=u+1 u=u+1

v=v−1 v=v−1
B4 B4 k=i
if v <= 20 goto B5 if v <= 20 goto B5 v=v−1 B4
if v <= 20 goto B5
j = i B5 j = i B5
j = i B5

Condition (a) Condition (b) Condition (c)


Detection of induction variables


A basic induction variable is a variable that only has assignments


of the form








Associated with each induction variable is a triple where





is a basic induction variable and and are constants such that










In this case belongs to the family of 

The basic induction variable belongs to its own family, with the





associated triple


Detection of induction variables (cont’d)

Find all basic induction variables in the loop


Find variables with a single assignment in the loop with one of the
following forms:






– , , , , , where





is a constant and  is an induction variable



If is not basic and in the family of then there must be





– No assignment of between the assignment of and




– No definition of outside the loop that reaches


Strength reduction for induction variables


Consider each basic induction variable in turn. For each variable





in the family of with triple :




– Create a new variable



– Replace the assignment to by





– Immediately after each assignment append












– Place in the family of with triple







– Initialize in the preheader:






Strength reduction for induction variables (cont’d)

B1 B1
i=m−1 i=m−1
t1 = 4 * n t1 = 4 * n
v = a[t1] v = a[t1]
s2 = 4 * i
B2
i=i+1 Strength reduction B2
t2 = 4 * i i=i+1
t3 = a[t2] s2 = s2 + 4
if t3 < v goto B2 t2 = s2
t3 = a[t2]
B3 if t3 < v goto B2
if i < n goto B5
B3
if i < n goto B5

B4 B5

B4 B5
Elimination of induction variables


Consider each basic induction variable only used to compute other
induction variables and tests









Take some in ’s family such that and from the triple




are simple

Rewrite tests if ( relop ) to


if ( relop )






Delete assignments to from the loop


Do some copy propagation to eliminate assignments formed



during strength reduction

You might also like