Code Generation and Optimization
Code Generation and Optimization
Generation
ISE Department
Objectives
To learn about
1. Issues In The Design Of Code Generators
3. Next-use Information
ISE Department
Code Generator
The final phase of the compiler
Code Optimization is the optional preceding
phase
Code generator should
Effectively use the features of target machine
and
Itself should run efficiently
Symbol
ISE Department Table
Optimization versus
Generation
Code generation:
usually transforms from one representation to
another
However, in as much as a code generator
selects among possibilities, it is also doing a
restricted form of optimization.
Optimization:
transforms a computation to an equivalent but
‘‘better’’ computation in the same
representation language
or
annotates the representation with additional
information
ISE Department
about the computation.
Optimization through
Transformation
Optimization process must have
following properties
It must preserve the meaning of programs
Must improve the performance by
measurable amount
It must be worth the effort spent by
compiler
1. Best program transformation must yield
the most benefit for the least effort
2. A fast and non-optimizing compiler may
be more useful during the debugging
phase and optimized one Control
at Flow
the Analysis
actual
runtime.
Available
ISE Department
Data Flow Analysis
options include:
Improving loops
Organization of Optimizing
Compiler
Code
Optimizer
Control Data
Flow Flow Transformation
Analysis Analysis s
ISE Department
Example: Finding Leaders
The following Code computes the
product of a
begin matrix
Sourc (1) prod := 0 Three
prod := 0; e (2) i := 1 Addr
i := 1; Code (3) t1 := 4 * i Code
do begin (4) t2 := a[t1]
prod := prod + a[i] * b[i]; (5) t3 := 4 * i
i = i+ 1; (6) t4 := b[t3]
end
while i <= 20 (7) t5 := t2 * t4
end RULES (8) t6 := prod + t5
1.The first statement in the program is a leader (9) prod := t6
2.Any statement that is the target of a branch (10) t7 := i + 1
statement is a leader (11) i := t7
3.Any statement that immediately follows a
ISE Department
(12)if i <= 20 goto (3)
branch or return statement is a leader (13)……….
Blocks
The basic block corresponding to a leader
consists of the leader, plus all statements
up to but not including the next leader or
up to the end of the program.
B1 (1) prod := 0 B2 (3) t1 := 4 * i B3 (13) …
(2) i := 1 (4) t2 := a[t1]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 := t2 * t4
(8) t6 := prod + t5
(9) prod := t6
(10) t7 := i + 1
ISE Department (11) i := t7
(12) if i <= 20 goto (3)
Flow Analysis
Blocks are used as basic units in Flow
Analysis
Control Flow Analysis: determine the
control structure of a program and build a
Control Flow Graph.
Data Flow Analysis: determine the flow of
scalar values and build Data Flow Graphs.
Control flow analysis Inter-procedural Program
ISE Department
void quicksort (m,n)
int m,n;
{
int i, j;
int v, z;
if ( n <= m ) return;
/* fragment begins here */
i := m-1; j := n; v := a [n] ;
Quick while (1) {
do i := I + 1; while ( a[ i ] < v );
Sort do j := j - 1; while ( a[ j ] > v );
if ( I >= j) break;
x := a[ i ]; a[ i ] = a[ j ]; a[ j ] := x;
}
x := a[ i ]; a[ i ] := a[n]; a[n] := x;
quicksort (m,j); quicksort ( i +1,n);
}
ISE Department
1. i := n-1 16.t7 := 4 * i
2. j := n 17. t8 := 4* j
3. t1 := 4*n 18.T9 := a [ t8 ]
4. v := a[t1] 19. a[t7] := t9
5. i := i + 1 20. t10 := 4 * j
6. t2 := 4* i 21. a[t10] :=x
7. t3 := a[ t2] 22. goto (5)
Corr. 8. if t1 < v goto(5) 23. t11 := 4 * i
Three 9. j := j-1 24. x := a[t11]
addre 10. t4 := 4 *j 25. t12 := 4 * i
11. t5 := a [t4] 26. t13 := 4 * n
ss 12. if t5 > v goto (9) 27. t14 := a[t13]
code 13. if i >= j goto 28. a[t11] := t14
(23) 29. t15 := 4 * n
ISE Department
14. t6 := 4 * i 30. a[t13] := x
1. i := n-1 14. t6 := 4 * i
2. j := n 15. x := a [t6]
B1 3. t1 := 4*n 16. t7 := 4 * i
17. t8 := 4* j
4. v := a[t1] 18. t9 := a [ t8 ] B5
5. i := i + 1 19. a[t7] := t9
6. t2 := 4* i 20. t10 := 4 * j
21. a[t10] :=x
Three B2 7. t3 := a[ t2] 22. goto (5)
8. if t1 < v goto(5)
addres 23. t11 := 4 * i
9. j := j-1 24. x := a[t11]
s 10. t4 := 4 *j 25. t12 := 4 * i
code 11. t5 := a [t4] 26. t13 := 4 * n B6
B3 27. t14 := a[t13]
12. if t5 > v goto (9)
28. a[t11] := t14
B4 13.if i >= j goto (23)
ISE Department 29. t15 := 4 * n
30. a[t13] := x
i := n-1
B1 j := n
t1 := 4*n FLOW
v := a[t1] GRAPH
B2
i := i + 1
Loops
t2 := 4* i
B5 t3 := a[ t2]
if t1 < v gotoB2 B6
t6 := 4 * i B
x := a [t6] 3
t11 := 4 * i
t7 := 4 * i j := j-1
x := a[t11]
t8 := 4* j t4 := 4 *j
t12 := 4 * i
T9 := a [ t8 ] t5 := a [t4] t13 := 4 * n
a[t7] := t9 if t5 > v goto B3 t14 := a[t13]
t10 := 4 * j a[t12] := t14
a[t10] :=x B4
t15 := 4 * n
goto B2 if i >= j goto B6 a[t15] := x
ISE Department
Try Transformations on this as an exercise
Principal Sources of Code
Optimization
1. Function Preserving Transformations
2. Common Sub-expressions
3. Copy Propagation
4. Dead Code Elimination
5. Loop Optimization
i. Code Motion
ii. Induction Variables &
iii. reduction in Strength
ISE Department
1. Structure Preserving
Transformations
a := b + c
1.1 Common Sub-expression Elimination b := a – d
Consider the code block c := b + c
The 2nd & 4th statement compute the d := a – d
same expression b + c - d a := b + c
Hence it can be replaced by b := a – d
c := b + c
d := b
ISE Department
i := n-1
B1 j := n Elimination of
t1 := 4*n Common Sub-ex
t2 := 4* i v := a[t1]
t3 := a[ t2]
B2 t2 := 4* i
t4 := 4 *j i := i + 1
t5 := a [t4] t4 := 4 *j
t2 := 4* i t1 := 4 *n
B5 t3 := a[ t2]
t6 := 4 * i if t1 < v gotoB2 B6
B
xx :=
:= t3
a [t6] 3
j := j-1 t11 := 4 * i
t7 := 4 * i x := a[t11]
t3
t8 := 4* j t4 := 4 *j
t12 := 4 * i
T9 := a [ t8 ] t5 := a [t4] t13 := 4 * n
a[t7]
a[t7]:=
:=t5
t9 if t5 > v goto B3 ]
t14 := a[t13]
t10 := 4 * j 2] := t14
a[t11]
a[t4] :=:=x
a[t10] x B4
t15 := 4 * n
goto B2 if i >= j goto B6 ] := x
a[t13]
ISE Department
1. Structure Preserving
Transformations
1.2 Dead Code Elimination
Eliminating code that computes a value
which is never used later !
Can be identified through:
1.Liveness analysis &
2.Def-Use Chains
const NN = 4;
….
i:=2+NN; i := 6
j:=i*5+a;
j := 30 + a
1.4 Loop Optimization
Aims to minimise the time spent in the
loop, often by reducing the number of
operations in the loop
Can be done in several ways
ISE Department
1. Structure Preserving
Transformations
1.4.1 Code Motion
Moving the invariant code out of the loop:
The following Code: – Can be rewritten as
for i := 1 to 10 t := b/c;
do for i := 1 to 10 do begin
begin z := i + t;
z := i + .
b/c end:
..
end;
Taking an expression and placing it outside
the loop that yields the result independent of
the number of times a loop is executed.
while ( j < limit-2 ) do …
can be written as
t := limit-2; while j < t do…
ISE Department
1. Structure Preserving
Transformations
1.4.2 Loop Unrolling
Reducing number of iterations
– The following Code:
Can be rewritten as
i:=1; i:=1;
while (i<=50) do begin while (i<=50) do begin
a[i]:= b[i]; i:= i + 1 a[i]:= b[i]; i:= i + 1;
end; a[i]:= b[i]; i:= i + 1;
end;
ISE Department
i := n-1
B1 j := n
t1 := 4*n Strength
v := t4 := 4
a[t1]; *j ; Reduction
B2
i := i + 1
The variables j and
t4 in B3 are in the t2 := 4* i
lock-step, in each t3 := a[ t2]
iteration, j is if t1 < v gotoB2
B
decremented by 1 3
and t4 by 4. j := j-1
B5 t4 := t4
4 *j- 4 B6
t5 := a [t4]
if t5 > v goto B3
B4
can be eliminated.
Statement
x := y ** 2 can be changed to cheaper x := y *
y
X := y * 2 can be changed to cheaper x := y +
y
( Strength Reduction )
ISE Department
ISE Department
Code Generator Issues
1. Input to code generator –Intermediate
Representations
Several choices including graphical ones
Assumption is prior stage has done type
checking & job to be done is allocating the
target machine variable types
Further assumption is error checking is
done thing
2. Target Code
Could be absolute machine code or
relocatable code or assembly language code
3. Memory management
Mapping of variables to memory locations
( absolutely or relatively
ISE Department
Code Generator Issues
(Contd.)
4. Instruction Selection
Decided by the nature of the instruction
set, supported data types, instruction
speeds machine idioms etc.... of the target
machine
Each three address code line can be
individually translated into target machine
code ; but this could result in poor code
5. Register Allocation
Instructions involving registers (RR) tend to
go fast
Effective use of variables in registers
speeds up the program execution
ISELimited
Department number of registers in the system
calls for efficient use of these register
ISE Department
Any
Questions ?
???
Thank you
ISE Department