0% found this document useful (0 votes)
35 views

Basic Blocks and Flow Graphs

Uploaded by

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

Basic Blocks and Flow Graphs

Uploaded by

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

Chapter 10

Code Optimization
• A main goal is to achieve a better performance

source
Front End Intermediate Code Gen target
Code
Code Code

user Machine-
independent Machine-
Compiler dependent
optimizer Compiler
optimizer
• Two levels

– Machine independent code opt


• Control Flow analysis
• Data Flow analysis
• Transformation

– Machine dependent code-op


• Register allocation
• Utilization of special instructions.
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
Quicksort
// assume an external input-output array: int a[]
void quicksort( int m, int n ) {
int i, j, v, x; // temps
if ( n <= m ) return;
i = m-1;
j = n;
v = a[n];
while(1) {
do i=i+1; while( a[i] < v );
do j=j-1; while( a[j] > v );
if ( i >= j ) break;
x = a[i]; a[i] = a[j]; a[j] = x;
} //end while
x = a[i]; a[i] =a [n]; a[n] = x;
quicksort( m, j );
quicksort( i+1, n );
} //end quicksort
Basic Block
Next-use information
• Next use information is used in code
generation and register allocation.
• Next use of A in quad i is j if
• Quad i: A = …(assignment to A)
(control flows from I to j with no assignments
to A )
• Quad j: = A op B (usage of A)
• In computing next use, we assume that on exit
from the basic block all temporaries are
considered non-live.
• All programmer defined variables (and non temp
variables) are live.
• Each procedure/function call is assumed to start
a basic block.
• Next use is computed on a backward scan on the
quads in a basic block, starting from the end.
• Next use information is stored in the symbol
table.
Three address code computing dot
product
(1)PROD:=0
(2)I:=1
(3)T1:=4*I
(4)T2:=addr(A)-4
(5)T3:= T2[T1]
(6)T4:= addr(B)-4
(7)T5:= T4[T1]
(8)T6:= T3*T5
(9)PROD:=PROD+T6
(10)I:=I+1
(11)If I ≤ 20 goto (3)
3 T1:=4*I T1-(nl, nu-5), I-(lv, nu-10)

4 T2:=addr(A)-4 T2-(nl, nu-5)

5 T3:= T2[T1] T3-( nl, nu-8), T2-(nl,nnu), T1-(nl,nu-7)

6 T4:= addr(B)-4 T4-(nl,nu-7)

7 T5:= T4[T1] T5-(nl,nu-8), T4-(nl, nnu), T1(nl, nnu)

8 T6:= T3*T5 T6-( nl,nu-9), T3-(nl, nnu), T5-(nl, nnu)

9 PROD:=PROD+T6 PROD-(lv, nnu), T6-(nl, nnu)

10 I:=I+1 I-(lv, nu-11)

11 If I ≤ 20 goto (3) I-(lv, nnu)


PROD:=0
B1
I:=1

T1:=4*I
T2:=addr(A)-4
T3:= T2[T1]
T4:= addr(B)-4 B2
T5:= T4[T1]
T6:= T3*T5
PROD:=PROD+T6
I:=I+1
If I ≤ 20 goto (3)
Quicksort
// assume an external input-output array: int a[]
void quicksort( int m, int n ) {
int i, j, v, x; // temps
if ( n <= m ) return;
i = m-1;
j = n;
v = a[n];
while(1) {
do i=i+1; while( a[i] < v );
do j=j-1; while( a[j] > v );
if ( i >= j ) break;
x = a[i]; a[i] = a[j]; a[j] = x;
} //end while
x = a[i]; a[i] =a [n]; a[n] = x;
quicksort( m, j );
quicksort( i+1, n );
} //end quicksort
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
potentially many iterations
• Look for expressions that yeild the same result
independent of the iterations.

• before after
• While ( I <= limit – 2). 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

You might also like