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

Code Generation and Optimization

The document discusses the design and implementation of code generators and optimization techniques in compilers. It covers topics such as basic blocks, flow graphs, register allocation, and various optimization strategies to improve code performance. The importance of control flow analysis and the construction of control flow graphs (CFG) for program optimization is also highlighted.

Uploaded by

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

Code Generation and Optimization

The document discusses the design and implementation of code generators and optimization techniques in compilers. It covers topics such as basic blocks, flow graphs, register allocation, and various optimization strategies to improve code performance. The importance of control flow analysis and the construction of control flow graphs (CFG) for program optimization is also highlighted.

Uploaded by

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

Optimization & Code

Generation

ISE Department
Objectives
 To learn about
1. Issues In The Design Of Code Generators

2. Basic Blocks And Flow Graphs

3. Next-use Information

4. A Simple Code Generator

5. Register Allocation & Assignment

6. The Dag Representation Of Basic Blocks

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

Source Front IR Code IR Code Target


Code End Code Optimizer Code Generato Code
r

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

1. Intermediate code can be relatively


independent of the target machine,
2. So the optimizer can perform freely and
does not have to change even if code
generator
ISE Department
is changed
Basic Blocks
 A sequence of consecutive intermediate
language statements in which flow of
control can only enter at the beginning
and leave at the end.
 A maximal length segment of straight-line
code (i.e., no branches)
 Basic Block terminology
1. The first statement in the block is a
LABEL / LEADER.
2. The last statement in the block is a JUMP
or a CJUMP.
3. There are no other LABELs, JUMPs, or
CJUMPs in the block.
 Question
ISE Department : Why do we need to look at
Blocks ?
Basic Block
 Significance
 If any statement executes, they all
execute
 Barring exceptions or other unusual
circumstances
 Execution totally ordered
 Many techniques for improving basic
blocks – simplest and strongest methods
 How to partition the IR code into Basic
blocks ?
1. Identify LEADER statements
2. The basic block corresponding to a leader
consists of the leader, plus all statements
up to but not including the next leader or
ISE Department
up to the end of the program
How to Identify the leader
?
 Using the following rules
1. The first statement in the program is a
leader

2. Any statement that is the target of a


branch statement is a leader (for most
intermediate languages these are
statements with an associated label)

3. Any statement that immediately follows a


branch or return statement is a leader

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

Flow analysis Intra-procedural Procedure

Local Basic block


Data flow analysis
ISE Department
B1 (1) prod := 0
Building CFG B1
(2) i := 1
B2 Rule (1)
Rule (2 i )
B3
(3) t1 := 4 * i
There is a directed (4) t2 := a[t1]
(5) t3 := 4 * i
edge from basic B2
(6) t4 := b[t3]
block Bx to basic (7) t5 := t2 * t4
block By in the (8) t6 := prod + t5
CFG if: (9) prod := t6
(1) There is a branch (10) t7 := i + 1
from the last (11) i := t7
statement of Bx to (12) if i <= 20 goto (3)
the first statement of
Rule (2 ii )
By,ISEor
Department
B3 (13) …
Control Flow Graph
 A control flow graph (CFG), or simply a flow
graph, is a directed multi-graph in which:
(i) the nodes are basic blocks; and
(ii)the edges represent flow of control
(branches/ fall-through )
 The basic block whose leader is the first
intermediate language statement is called
the start node
 No information about the data;
 Therefore an edge in the CFG means that
the program may take that path
 How to build CFG ?
ISE Department
Use of CFG’s
Used to identify loops in code with several
blocks, with two concepts
Strongly Connected nodes
a collection of N nodes where there is a path (
of length one or more ) from any node in N to
any ther node in N
Dominance
A node a in a CFG dominates a node b if every
path from the start node to node b goes
through a. We say that node a is a dominator
of node b
Motivation:
Programs spend most of the execution time in
loops, therefore there is a larger payoff for
optimizations that exploit loop structure
ISE Department
Basic Block
Transformations
 Basic Block Computes a set of expressions
 Number of transformations can be applied
to a basic block without changing the
expressions computed by the block
 Such transformations are useful in Code
optimization since they can help in
 Improving the running time and
 Saving the memory occupied by the block
( program)
 Two types of transformations are possible
1. Structure preserving &
2. Algebraic Transformations

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

If ( debug ) print is usually preceded by an


assignment debug := false. If copy
propagation replaces debug by false,
then the print statement is dead since it
can’t be reached. We can eliminate both
test and print statements
More generally, deducing at compile time
that the value of an expression is a
ISE Department
f := g is copy statement or simply a
copy. In fact, elimination of common
sub-expressions introduces some of these copy
statements. It would be incorrect to replace c:=
d+e with c := a or c := b as one does not know
the flow of control at the run time. The idea is
x := t3
to use g for f wherever possible after the copy
a[t2] := t5
statement f := g in B5 of the example,
a[t4] := x
goto B2 which can be replaced by
Copy x := t3
a[t2] := t5
Propagation a[t4] := t3
ISE Department goto B2
This may not appear to be an improvement, but it
1. Structure Preserving
Transformations
1.3 Constant propagation ( Constant folding)
 Constant expressions are calculated and
used
 Consider the following Code:

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

ISE Department if i >= j goto B6


1. Structure Preserving
Transformations
1.4.3 Induction variable Elimination
 Consider the
 Certain variables, in a loop derive their
values from the looping variable ( i or
Following
Code;
1. j := j-1
j ) and hence their values form an
arithmetic progression as we go 2. t4 := 4 *j
around the loop. 3. t5 := a [t4]
 All such induction variables must be 4. if t5 > v goto (1)
related by a linear function at each
point in the loop.  It can be replaced by
 The calculation of each induction
1. t4 := t4 - 4
variable, frequently involving
multiplication, may be replaced by 2. t5 := a [t4]
addition to its former value. 3. if t5 > v goto (1)
ISE Department
 Such improvements, replacing an
i := n-1
B1
j := n
t1 := 4*n FLOW
v := a[t1]; t4 := 4GRAPH
*j ; t2 := 4*i
B2
t2 := t2 + 4 AFTER LOOP
t3 := a[ t2]
if t1 < v gotoB2
OPTIMIZATI
ON
B
3
t4 := t4 - 4
B5 t5 := a [t4] B6
a[t2] := t5 if t5 > v goto B3 t14 := a[t1]
a[t4] := t3 a[t2] := t14
goto B2 a[t1] := t3
B4

ISE Department if t2 >= t4 goto B6


2. Algebraic
Transformation
Can be used to change the set of
expressions by its algebraic equivalent
For Example statements like
 x := x = 0 or
 x := X * 1

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

You might also like