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

Unit VI Code Optimization

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit VI Code Optimization

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

UNIT VI : CODE OPTIMIZATION AND CODE

GENERATION
COURSE : LANGUAGE PROCESSOR AND COMPILER CONSTRUCTION

Manisha Mali
[email protected]

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48


(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)
Department of Computer Engineering
Unit VI
Code optimization
THE PHASES OF A COMPILER
Source program

Lexical Analyzer

26-Oct-20
Syntax Analyzer

VIIT
Manisha Mali Computer Engg. Dept.,
Semantic Analyzer
Symbol Error
table handler
manager
Intermediate code generator

Code optimizer

Code generator
3

Target
program
CODE OPTIMIZATION

26-Oct-20
 Code optimization organization
 Optimization

VIIT
Manisha Mali Computer Engg. Dept.,
 Machine dependent/independent

 Criteria for Transformations

 Principal sources of optimization

 Peephole optimization

Manisha Mali
Manisha Mali Computer Engg. Dept.,
26-Oct-20
VIIT
5
CODE OPTIMIZATION AND PHASES

Manisha Mali
OPTIMIZATION

 Code produced by standard algorithms can

26-Oct-20
often be made to run faster, take less space or
both

VIIT
Manisha Mali Computer Engg. Dept.,
 Theseimprovements are achieved through
transformations called optimizations

 Compilers that apply these transformations


are called optimizing compilers

Manisha Mali
OPTIMIZING INTERMEDIATE CODE
 This phase is generally only included in

26-Oct-20
optimizing compilers
 Offers the following advantages:

VIIT
Manisha Mali Computer Engg. Dept.,
– 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
7

Manisha Mali
FACTORS INFLUENCING OPTIMIZATION

26-Oct-20
 The target machine: machine dependent
factors can be parameterized to compiler
for fine tuning

VIIT
Manisha Mali Computer Engg. Dept.,
 Architecture of Target CPU:
 Number of CPU registers
 RISC vs CISC
 Pipeline Architecture
 Number of functional units
 Machine Architecture
 Cache Size and type
 Cache/Memory transfer rate
8
MACHINE DEPENDENT/INDEPENDENT
 Machine Independent

26-Oct-20
 Common Subexpression elimination
 removing of loop invariants
Reduction in strengths.

VIIT
Manisha Mali Computer Engg. Dept.,

 dead code elimination,
 code motion
 Machine dependent Issues
 Assignment and use of registers
 latency hiding; managing bounded machine resources like
registers, functional units, caches.
 Rearrangement of Quadruples for code
optimization
9

Manisha Mali
CRITERIA FOR TRANSFORMATIONS

26-Oct-20
 Criteria for Code-Improving Transformation:
 Meaning must be preserved (correctness)
 Speedup must occur on average.

VIIT
Manisha Mali Computer Engg. Dept.,
 Work done must be worth the effort
 Cannot change the output produced for any
 Can not introduce an error

 Opportunities:
 Programmer (algorithm, directives)
 Intermediate code
 Target code
10
BEYOND OPTIMIZING COMPILERS
 Improvements can be made at various phases
 Source code:

26-Oct-20
– Algorithmic transformations can produce spectacular
improvements
– Profiling can be helpful to focus a programmer's

VIIT
Manisha Mali Computer Engg. Dept.,
attention on important code
 Intermediate code:
– Compiler can improve loops, procedure calls, and
address
calculations
– Typically only optimizing compilers include this phase
 Target code:
– Compilers can use registers efficiently
– Peephole transformation can be applied
11

Manisha Mali
THEMES BEHIND OPTIMIZATION
TECHNIQUES

26-Oct-20
 Avoid redundancy: something already
computed need not be computed again
 Smaller code: less work for CPU, cache, and

VIIT
Manisha Mali Computer Engg. Dept.,
memory!
 Less jumps: jumps interfere with code pre-fetch
 Code locality: codes executed close together in
time is generated close together in memory –
increase locality of reference
 Extract more information about code: More
info – better code generation

12
PRINCIPAL SOURCES OF
OPTIMIZATION

26-Oct-20
 Function-preserving transformations
 Common subexpressions

VIIT
Manisha Mali Computer Engg. Dept.,
 Copy propogation
 Dead-code elimination

 Loop optimization
 Code motion
 Induction variables
 Reduction in strength

13

Manisha Mali
OPTIMIZING TRANSFORMATIONS

26-Oct-20
 Compile time evaluation
 Constant folding

VIIT
Manisha Mali Computer Engg. Dept.,
 Constant propagation
 Common sub-expression elimination
 Code motion
 Strength Reduction
 Dead code elimination
 Copy propagation
 Loop optimization
 Induction variables and strength reduction 14
COMPILE-TIME EVALUATION

26-Oct-20
 Expressions whose values can be pre-computed
at the compilation time

VIIT
Manisha Mali Computer Engg. Dept.,
 Two ways:
 Constant folding
 Constant propagation

15
COMPILE-TIME EVALUATION

26-Oct-20
 Constant folding: Evaluation of an expression
with constant operands to replace the expression
with single value

VIIT
Manisha Mali Computer Engg. Dept.,
 Example:
area := (22.0/7.0) * r ** 2

area := 3.14286 * r ** 2

16
CONSTANT PROPAGATION
 What does it mean?
 Given an assignment x = c, where c is a constant,

26-Oct-20
replace later uses of x with uses of c, provided there are
no intervening assignments to x.
Similar to copy propagation

VIIT
Manisha Mali Computer Engg. Dept.,

 Extra feature: It can analyze constant-value conditionals to


determine whether a branch should be executed or not.
 When is it performed?
 Early in the optimization process.
 What is the result?
 Smaller code
 Fewer registers

17
COMPILE-TIME EVALUATION

26-Oct-20
 Constant Propagation: Replace a variable with
constant which has been assigned to it earlier.

VIIT
Manisha Mali Computer Engg. Dept.,
 Example:
pi := 3.14286
area = pi * r ** 2

area = 3.14286 * r ** 2

18
CLASSIFICATIONS OF
OPTIMIZATION TECHNIQUES

26-Oct-20
 Peephole optimization
 Local optimizations

VIIT
Manisha Mali Computer Engg. Dept.,
 Global Optimizations
 Inter-procedural
 Intra-procedural

 Loop optimization

19
PEEPHOLE OPTIMIZATIONS

1. A Simple but effective technique for locally improving the

26-Oct-20
target code (can also be applied to intermediate code) is
peephole optimization,
2. A method for trying to improve the performance of the target

VIIT
Manisha Mali Computer Engg. Dept.,
program
3. The peephole is a small, moving window on the target program
4. By examining a short sequence of target instructions and
replacing these instructions by a shorter or faster sequence
whenever possible.
5. Each improvement may create opportunities for additional
improvements
6. Repeated passes may be necessary
20
PEEPHOLE OPTIMIZATIONS

Characteristics of peephole optimization

26-Oct-20
1. Redundant instruction elimination
2. Flow of control information

VIIT
Manisha Mali Computer Engg. Dept.,
3. Algebraic Simplification
4. Use of machine Idioms

21

Manisha Mali
SOME PEEPHOLE OPTIMIZATIONS
 A few algebraic identities that occur frequently (such as
x:= x + 0 or x := x * 1) can be eliminated

26-Oct-20
 Reduction in strength replaces expensive operations
with cheaper ones
– Calculating x * x is likely much cheaper than x^2 using

VIIT
Manisha Mali Computer Engg. Dept.,
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
straightforward x := x + 1
– Auto-increment and auto-decrement are also useful
when pushing into or popping off of a stack

22

Manisha Mali
ALGEBRAIC IDENTITIES

26-Oct-20
 Worth recognizing single instructions with a constant
operand:

VIIT
Manisha Mali Computer Engg. Dept.,
A * 1 = A
A * 0 = 0
A / 1 = A
A * 2 = A + A
More delicate with floating-point
 Strength reduction:
A ^ 2 = A * A

23
PEEPHOLE OPTIMIZATIONS (CONT’D)
REDUNDANT-INSTRUCTION ELIMINATION

26-Oct-20
 Redundant loads and stores
1. lw $t0, a

VIIT
Manisha Mali Computer Engg. Dept.,
2. sw $t0, a
 Unreachable code
#define debug 0
if (debug) {
/* print debugging information */
}

24

Manisha Mali
PEEPHOLE OPTIMIZATIONS
Constant Folding

26-Oct-20

x := 32 becomes x := 64
x := x + 32

VIIT
Manisha Mali Computer Engg. Dept.,
 Unreachable Code
goto L2
x := x + 1  No need
 Flow of control optimizations
goto L1 becomes goto L2

L1: goto L2  No needed if no other L1 branch

25
PEEPHOLE OPTIMIZATIONS

26-Oct-20
 Algebraic Simplification
x := x + 0  No needed
Dead code

VIIT
Manisha Mali Computer Engg. Dept.,

x := 32  where x not used after statement
y := x + y  y := y + 32
 Reduction in strength
x := x * 2  x := x + x
 x := x << 2

26
Common expression
can be eliminated

26-Oct-20
SIMPLE EXAMPLE: A[I+1] = B[I+1]

VIIT
Manisha Mali Computer Engg. Dept.,
t1 = i+1 t1 =i+1
t2 = b[t1] t2 = b[t1]
t3 = i + 1  no longer live
t3 = i + 1
a[t1] = t2
a[t3] = t2

27
Now, suppose i is a constant:

26-Oct-20
i =4 i =4 i=4
t1 = i+1 t1 =5 t1 = 5

VIIT
Manisha Mali Computer Engg. Dept.,
t2 = b[t1] t2 = b[t1] t2 = b[5]
a[t1] = t2 a[t1] = t2 a[5] = t2

i=4
Final Code:
t2 = b[5]
a[5] = t2

28
OPTIMIZATIONS ON CFG

26-Oct-20
 Must take control flow into account
 Common Sub-expression Elimination

VIIT
Manisha Mali Computer Engg. Dept.,
 Constant Propagation
 Dead Code Elimination
 Partial redundancy Elimination
 …
 Applying one optimization may raise
opportunities for other optimizations.

29
SIMPLE LOOP OPTIMIZATIONS

26-Oct-20
 Code Motion
Move invariants out of the loop.

VIIT
Manisha Mali Computer Engg. Dept.,
Example:
while (i <= limit - 2)
becomes
t := limit - 2
while (i <= t)

30
PRINCIPAL SOURCES OF
OPTIMIZATION

26-Oct-20
 Function-preserving transformations
 Common subexpressions

VIIT
Manisha Mali Computer Engg. Dept.,
 Copy propogation
 Dead-code elimination

 Loop optimization
 Code motion
 Induction variables
 Reduction in strength

31

Manisha Mali
BASIC BLOCK

26-Oct-20
 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

VIIT
Manisha Mali Computer Engg. Dept.,
branching except at the end

 Flow Graph
 Once an intermediate code program is
partitioned into basic blocks, we represent the
flow of control between them by a flow graph

32
E X.

26-Oct-20
fact(x)
{

VIIT
Manisha Mali Computer Engg. Dept.,
int f=1;
for(i=2;i<=x;i++)
f=f*i;
return(f);
}

33
QUICKSORT IN C
void quicksort(m,n)
int m, n;
{

26-Oct-20
int i,j;
int v,x;
if ( n <= m ) return;
/* fragment begins here */

VIIT
Manisha Mali Computer Engg. Dept.,
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;
}
x = a[i]; a[i] = a[n]; a[n] = x; /* fragment ends here
*/
quicksort (m, j); quicksort (i+1 ,n);
}
34

Manisha Mali
Manisha Mali Computer Engg. Dept.,
26-Oct-20
VIIT
35
BASIC BLOCK
THREE ADDRESS CODE OF QUICK SORT

1 i=m-1 16 t7 = 4 * i
2 j=n 17 t8 = 4 * j

26-Oct-20
3 t1 =4 * n 18 t9 = a[t8]
4 v = a[t1] 19 a[t7] = t9

VIIT
Manisha Mali Computer Engg. Dept.,
5 i=i +1 20 t10 = 4 * j
6 t2 = 4 * i 21 a[t10] = x
7 t3 = a[t2] 22 goto (5)
8 if t3 < v goto (5) 23 t11 = 4 * i
9 j=j–1 24 x = a[t11]
10 t4 = 4 * j 25 t12 = 4 * i
11 t5 = a[t4] 26 t13 = 4 * n
12 if t5 > v goto (9) 27 t14 = a[t13]
13 if i >= j goto (23) 28 a[t12] = t14
14 t6 = 4 * i 29 t15 = 4 * n
36
15 x = a[t6] 30 a[t15] = x
FIND THE BASIC BLOCK

1 i=m-1 16 t7 = 4 * I
2 j=n 17 t8 = 4 * j

26-Oct-20
3 t1 =4 * n 18 t9 = a[t8]
4 v = a[t1] 19 a[t7] = t9

VIIT
Manisha Mali Computer Engg. Dept.,
5 i=i +1 20 t10 = 4 * j
6 t2 = 4 * i 21 a[t10] = x
7 t3 = a[t2] 22 goto (5)
8 if t3 < v goto (5) 23 t11 = 4 * i
9 j=j–1 24 x = a[t11]
10 t4 = 4 * j 25 t12 = 4 * i
11 t5 = a[t4] 26 t13 = 4 * n
12 if t5 > v goto (9) 27 t14 = a[t13]
13 if i >= j goto (23) 28 a[t12] = t14
14 t6 = 4 * i 29 t15 = 4 * n
37
15 x = a[t6] 30 a[t15] = x
B1 FLOW GRAPH
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t7 = 4 * i t12 = 4 * i
t3 = a[t2] t8 = 4 * j t13 = 4 * n
if t3 < v goto B2 t9 = a[t8] t14 = a[t13]
B3 a[t7] = t9 a[t12] = t14
j=j–1 t10 = 4 * j t15 = 4 * n
t4 = 4 * j
a[t10] = x a[t15] = x
t5 = a[t4]
goto B2
if t5 > v goto B3

B4
38
if i >= j goto B6
LOCAL VS. GLOBAL
TRANSFORMATIONS

26-Oct-20
 Optimizations within a basic block is called local
optimization.
 Optimizations across basic blocks is called global

VIIT
Manisha Mali Computer Engg. Dept.,
optimization.
 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 39

Manisha Mali
COMMON SUBEXPRESSIONS

26-Oct-20
 E is a common subexpression if:
– E was previously computed

VIIT
Manisha Mali Computer Engg. Dept.,
– 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

40

Manisha Mali
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t7 = 4 * i t12 = 4 * i
t3 = a[t2] t8 = 4 * j t13 = 4 * n
if t3 < v goto B2 t9 = a[t8] t14 = a[t13]
B3 a[t7] = t9 a[t12] = t14
j=j–1 t10 = 4 * j t15 = 4 * n
t4 = 4 * j
a[t10] = x a[t15] = x
t5 = a[t4]
goto B2
if t5 > v goto B3

B4
41
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 t10 = 4 * j a[t12] = t14
j=j–1 a[t10] = x t15 = 4 * n
t4 = 4 * j
goto B2 a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
42
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 *i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 a[t8] = x a[t12] = t14
j=j–1 goto B2 t15 = 4 * n
t4 = 4 * j
a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
43
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 a[t8] = x a[t12] = t14
j=j–1 goto B2 t15 = 4 * n
t4 = 4 * j
a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
44
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6] x = a[t11]
t2 = 4 * i t8 = 4 * j t13 = 4 * n
t3 = a[t2] t9 = a[t8] t14 = a[t13]
if t3 < v goto B2 a[t6] = t9 a[t11] = t14
B3 a[t8] = x t15 = 4 * n
j=j–1 goto B2 a[t15] = x
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
45
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6]
x = a[t11]
t2 = 4 * i t8 = 4 * j
t13 = 4 * n
t3 = a[t2] t9 = a[t8]
t14 = a[t13]
if t3 < v goto B2 a[t6] = t9
a[t11] = t14
B3 a[t8] = x
a[t13] = x
j=j–1 goto B2
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
46
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 t6 = 4 * i
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 x = a[t6]
x = a[t11]
t2 = 4 * i t8 = 4 * j
t13 = 4 * n
t3 = a[t2] t9 = a[t8]
t14 = a[t13]
if t3 < v goto B2 a[t6] = t9
a[t11] = t14
B3 a[t8] = x
a[t13] = x
j=j–1 goto B2
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
47
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = a[t2]
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 t8 = 4 * j
x = a[t11]
t2 = 4 * i t9 = a[t8]
t13 = 4 * n
t3 = a[t2] a[t2] = t9
t14 = a[t13]
if t3 < v goto B2
a[t8] = x
a[t11] = t14
B3 goto B2
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
48
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 t8 = 4 * j
x = a[t11]
t2 = 4 * i t9 = a[t8]
t13 = 4 * n
t3 = a[t2] a[t2] = t9
t14 = a[t13]
if t3 < v goto B2
a[t8] = x
a[t11] = t14
B3 goto B2
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
49
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 t9 = a[t4]
x = a[t11]
t2 = 4 * i a[t2] = t9
t13 = 4 * n
t3 = a[t2] a[t4] = x
t14 = a[t13]
if t3 < v goto B2
goto B2
a[t11] = t14
B3
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
50
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 a[t2] = t5
x = a[t11]
t2 = 4 * i a[t4] = x
t13 = 4 * n
t3 = a[t2] goto B2
t14 = a[t13]
if t3 < v goto B2
a[t11] = t14
B3
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
51
if i >= j goto B6
B1 COMMON SUBEXPRESSION ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = t3
x = t3

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 a[t2] = t5
t14 = a[t1]
t2 = 4 * i a[t4] = x
a[t2] = t14
t3 = a[t2] goto B2
a[t1] = x
if t3 < v goto B2

B3
j=j–1
Similarly for B6
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
52
if i >= j goto B6
COPY PROPAGATION
 Assignments of the form f := g are called copy

26-Oct-20
statements (or copies)
 The idea behind copy propagation is to use g
for f whenever possible after such a statement

VIIT
Manisha Mali Computer Engg. Dept.,
 For example, applied to block B5 of the
previous flow graph, we obtain:
x := t3
a[t2] := t5
a[t4] := t3
goto B2
 Copy propagation often turns the copy
statement into "dead code" 53

Manisha Mali
COPY PROPAGATION

26-Oct-20
f := g are called copy statements or copies
 Use of g for f, whenever possible after copy

VIIT
Manisha Mali Computer Engg. Dept.,
statement

Example:
x[i] = a; x[i] = a;
sum = x[i] + a; sum = a + a;

 Maynot appear to be code improvement, but


opens up scope for other optimizations.
54
COPY PROPAGATION (CONT’D)

26-Oct-20
 Copies introduced during common subexpression
elimination

VIIT
Manisha Mali Computer Engg. Dept.,
55

Manisha Mali
DEAD CODE ELIMINATION
A variable is live at a point in a program if its
value can be used subsequently; otherwise, it is

26-Oct-20
dead at that point.
 Dead code includes code that can never be

VIIT
Manisha Mali Computer Engg. Dept.,
reached and
code that computes a value that never gets used
 Deducing at compile time that the value of an
expression is a constant and using the constant
instead is known as constant folding
 Data flow analysis

 E.g. debug = false;


if (debug) printf(“Welcome”); 56

Manisha Mali
B1 DEAD CODE ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 x = t3
x = t3

VIIT
Manisha Mali Computer Engg. Dept.,
i=i +1 a[t2] = t5
t14 = a[t1]
t2 = 4 * i a[t4] = x
a[t2] = t14
t3 = a[t2] goto B2
a[t1] = x
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
57
if i >= j goto B6
B1 DEAD CODE ELIMINATION
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 a[t2] = t5 t14 = a[t1]

VIIT
Manisha Mali Computer Engg. Dept.,
a[t4] = t3 a[t2] = t14
i=i +1
t2 = 4 * i goto B2 a[t1] = t3
t3 = a[t2]
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
58
if i >= j goto B6
LOOP OPTIMIZATIONS
 Induction variables: variables that remain in
"lock-step"
– For example, in block B3 of previous flow graph, j and

26-Oct-20
t4 are induction variables
– Induction-variable elimination can sometimes
eliminate all but one of a set of induction variables

VIIT
Manisha Mali Computer Engg. Dept.,
 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 59

Manisha Mali
CODE MOTION
 An important modification that decreases the
amount of code in a loop

26-Oct-20
 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

VIIT
Manisha Mali Computer Engg. Dept.,
– Increase the number of statements in the outer loop
 Code motion moves code outside of a loop

 E.g while ( i <= limit – 2 ) /* stmt does not change limit */


Code motion will result in the equivalent result of
t = limit – 2;
While (i <= t) /* stmt does not change limit or t */
60

Manisha Mali
STRENGTH REDUCTION

26-Oct-20
 Replacement of an operator with a less costly one.
Example:

VIIT
Manisha Mali Computer Engg. Dept.,
temp = 5;
for i=1 to 10 do for i=1 to 10 do
… …
x=i*5 x = temp
… …
temp = temp + 5
end end
• Typical cases of strength reduction occurs in address
calculation of array references.
• Applies to integer expressions involving induction
variables (loop optimization)
61
B1 REDUCTION IN STRENGTH
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
B2 a[t2] = t5 t14 = a[t1]

VIIT
Manisha Mali Computer Engg. Dept.,
a[t4] = t3 a[t2] = t14
i=i +1
t2 = 4 * i goto B2 a[t1] = t3
t3 = a[t2]
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
62
if i >= j goto B6
B1 REDUCTION IN STRENGTH
i=m-1
j=n
t1 =4 * n

26-Oct-20
v = a[t1]
B5 B6
t2 = 4 * i
B2 t4 = 4 * j
a[t2] = t5 t14 = a[t1]

VIIT
Manisha Mali Computer Engg. Dept.,
a[t4] = t3 a[t2] = t14
goto B2 a[t1] = t3
t2 = t2 + 4
t3 = a[t2]
if t3 < v goto B2
B3

t4 = t4 - 4
t5 = a[t4]
if t5 > v goto B3

B4
63
if i >= j goto B6
EXERCISE

26-Oct-20
Read following piece of code: [4 Mks]
B1: j = j-1

VIIT
Manisha Mali Computer Engg. Dept.,
t4 = 4*j
t5 = a[t4]
if t5>j go to B1
which optimization technique can be applied to
this code? Explain it in detail.

64
EXERCISE 3
Consider the following code that produces a 10*10 identity
matrix.[12 Mks]
for (i=1; i=10; i++)
{

26-Oct-20
for (j=1; j=10; j++)
{
A[i, j] = 0;

VIIT
Manisha Mali Computer Engg. Dept.,
}}
for (i=1; i=10; i++)
{
A[i, i]=1;
}
Construct the
i) Three address code.
ii) Basic blocks.
iii) Flow graph.

 What kind of code optimization can you suggest for the above 65
code apply the same on the given code? [10 Mks]
EXERCISE 4
Perform the following the code fragment given below. [10 mks]
for(i=1;i=n;i++)
{
for(j=1;j=n;j++)

26-Oct-20
{
C[i,j]=0;

VIIT
Manisha Mali Computer Engg. Dept.,
}}
for(i=1;i=n;i++)
{
for(j=1;j=n;j++)
{
For(k=1;k=n;k++)
{
C[i,j]=C[i,j] + A[i, k]*B[k,j];
}}}

i) Partition into basic block


66
ii) Find loops in basic block
iii) Move the loop invariants outside the loop.
REFERENCES
 D. M. Dhamdhere, Systems Programming and Operating
Systems, Tata McGraw-Hill, ISBN 13:978-0-07-463579-7,
Second Revised Edition

Engineering, VIIT , Pune-48


Manisha Mali, Department of Computer
 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Compilers
Principles, Techniques and Tools, Addison Wesley,
ISBN:981–235–885 - 4, Low Price Edition
 J. J. Donovan, Systems Programming, McGraw-Hill, ISBN
13:978-0-07-460482-3, Indian Edition

67
26-Oct-20
Manisha Mali, Dept. Computer Engg., VIIT
68

You might also like