Você está na página 1de 59

Code Optimization

11/11/16

Rajkumar S ,SCSE
Code optimization

Introduction
Compiler code must be made to run fast and take less
space
Criteria for Code-Improving Transformation:
Meaning must be preserved (correctness)
Speedup must occur on average.
Work done must be worth the effort.

11/11/16

Rajkumar S ,SCSE
Code optimization

Organization
Control flow analysis
Data flow analysis
Code transformation

11/11/16

Rajkumar S ,SCSE
Code optimization

void quicksort(int m,int n)


{
int i ,j;
int v,x;
If (n<=m) return
/* partition */
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;
/* swap two elements */
x = a[i]; a[i] = a[j]; a[j] = x;
}
x = a[i]; a[i] = a[n]; a[n] = x;
/* Partition ends here */
quicksort(m, j); quicksort(i+1, n);
}

11/11/16

Rajkumar S ,SCSE
Code optimization

Three Address Code of Quick Sort


1

i=m-1

16

t7 = 4 * i

j=n

17

t8 = 4 * j

t1 =4 * n

18

t9 = a[t8]

v = a[t1]

19

i=i +1

20

a[t7] = t9

t2 = 4 * i

21

t3 = a[t2]

22

8
9
10
11

if t3 < v goto (5)


j=j1
t4 = 4 * j

23

t10 = 4 * j
a[t10] = x
goto (5)

24

t11 = 4 * I

25

x = a[t11]

26

t12 = 4 * i

27

t13 = 4 * n

12

t5 = a[t4]

13

if t5 > v goto (9)

28

t14 = a[t13]

14

if i >= j goto (23)

29

15

t6 = 4 * i

a[t12] = t14

30

t15 = 4 * n

11/11/16

x = a[t6]

Rajkumar S ,SCSE
Code optimization

a[t ] = x

Peephole Optimizations (9.9)


1.

A Simple but effective technique for locally improving the target code is
peephole optimization,

2.

a method for trying to improve the performance of the target program

3.

by examining a short sequence of target instructions and replacing these


instructions by a shorter or faster sequence whenever possible.

Characteristics of peephole optimization


1. Redundant instruction elimination
2. Flow of control information
3. Algebraic Simplification
4. Use of machine Idioms

11/11/16

Rajkumar S ,SCSE
Code optimization

Peephole Optimizations
Constant Folding
x:=32
x:=x+32

becomes

x:=64

Unreachable Code
gotoL2
x:=x+1 No need

Flow of control optimizations


gotoL1
becomes
gotoL2

L1:gotoL2 No needed if no other L1 branch

11/11/16

Rajkumar S ,SCSE
Code optimization

Peephole Optimizations
Algebraic Simplification
x:=x+0 No needed

Dead code
x:=32 where x not used after statement
y:=x+y
y:=y+32

Reduction in strength
x:=x*2

11/11/16

x:=x+x
x := x << 2

Rajkumar S ,SCSE
Code optimization

Basic Block Level


1. Common Sub expression elimination
2. Constant Propagation
3. Copy Propagation
4. Dead code elimination
5. Constant folding
Loop optimization
1. Code motion
2. Induction variable and reduction in strength

11/11/16

Rajkumar S ,SCSE
Code optimization

Basic blocks

I/p : A sequence of three-address statements


O/p : A list of basic blocks with each three address statement in exactly
one block
Method
1. We first determine the set of leaders, the first statements of basic
blocks. The rules we use are the followings:
i) The first statement is a leader
ii) Any statement that is the target of a conditional or unconditional goto
is a leader
iii) Any statement that immediately follows a goto or conditional goto
statement is a leader
2. For each leader, its basic block consists of the leader and all
statements up to but not including the next leader or the end of the
program
11/11/16

Rajkumar S ,SCSE
Code optimization

10

begin
prod =0;
i=1;
do begin
prod = prod + a[i] * b[i];
i=i+1;
end
while i<=20
end

11/11/16

Rajkumar S ,SCSE
Code optimization

11

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

Prod = 0
i =1
T1 = 4 * i
T2 = a[T1]
T3 = 4 * i
T4 = b[T3]
T5 = T2 * T4
T6 = prod + T5
Prod = T6
T7 = i+1
i = T7
if i<=20 goto 3

11/11/16

Rajkumar S ,SCSE
Code optimization

12

Common expression can be eliminated


Simple example: a[i+1] = b[i+1]
t1 = i+1
t2 = b[t1]
t3 = i + 1
a[t3] = t2

11/11/16

t1 = i + 1
t2 = b[t1]
t3 = i + 1 no longer live
a[t1] = t2

Rajkumar S ,SCSE
Code optimization

13

Now, suppose i is a constant:


i=4
t1 = i+1
t2 = b[t1]
a[t1] = t2

Final Code:

11/11/16

i=4
t1 = 5
t2 = b[t1]
a[t1] = t2

i=4
t1 = 5
t2 = b[5]
a[5] = t2

i=4
t2 = b[5]
a[5] = t2

Rajkumar S ,SCSE
Code optimization

14

Copy Propagation
a=d + e

b=d + 3
c=d + e

11/11/16

Rajkumar S ,SCSE
Code optimization

15

Dead code Elimination


A variable is live at point in a program if its value can be used
subsequently. Otherwise it is dead at that point (useless code)

11/11/16

Rajkumar S ,SCSE
Code optimization

16

Example

11/11/16

Rajkumar S ,SCSE
Code optimization

17

11/11/16

Rajkumar S ,SCSE
Code optimization

18

Constant Propagation

11/11/16

Rajkumar S ,SCSE
Code optimization

19

11/11/16

Rajkumar S ,SCSE
Code optimization

20

Copy propagation

11/11/16

Rajkumar S ,SCSE
Code optimization

21

Common sub exp elimination

11/11/16

Rajkumar S ,SCSE
Code optimization

22

11/11/16

Rajkumar S ,SCSE
Code optimization

23

11/11/16

Rajkumar S ,SCSE
Code optimization

24

Dead code elimination

11/11/16

Rajkumar S ,SCSE
Code optimization

25

11/11/16

Rajkumar S ,SCSE
Code optimization

26

Loop invariant

11/11/16

Rajkumar S ,SCSE
Code optimization

27

11/11/16

Rajkumar S ,SCSE
Code optimization

28

Reduction in strength

11/11/16

Rajkumar S ,SCSE
Code optimization

29

11/11/16

Rajkumar S ,SCSE
Code optimization

30

11/11/16

Rajkumar S ,SCSE
Code optimization

31

Optimizations on CFG
Must take control flow into account

Common Sub-expression Elimination


Constant Propagation
Dead Code Elimination
Partial redundancy Elimination

Applying one optimization may raise opportunities for other


optimizations.

11/11/16

Rajkumar S ,SCSE
Code optimization

32

Simple Loop Optimizations


Code Motion
Move invariants out of the loop.
Example:
while(i<=limit2)
becomes

t:=limit2
while(i<=t)

11/11/16

Rajkumar S ,SCSE
Code optimization

33

Three Address Code of Quick Sort


1

i=m-1

16

t7 = 4 * I

j=n

17

t8 = 4 * j

t1 =4 * n

18

t9 = a[t8]

v = a[t1]

19

i=i +1

20

a[t7] = t9

t2 = 4 * i

21

t3 = a[t2]

22

8
9
10
11

if t3 < v goto (5)


j=j1
t4 = 4 * j

23

t10 = 4 * j
a[t10] = x
goto (5)

24

t11 = 4 * I

25

x = a[t11]

26

t12 = 4 * i

27

t13 = 4 * n

12

t5 = a[t4]

13

if t5 > v goto (9)

28

t14 = a[t13]

14

if i >= j goto (23)

29

15

t6 = 4 * i

a[t12] = t14

30

t15 = 4 * n

11/11/16

x = a[t6]

Rajkumar S ,SCSE
Code optimization

a[t ] = x

34

Find The Basic Block


1

i=m-1

16

t7 = 4 * I

j=n

17

t8 = 4 * j

t1 =4 * n

18

t9 = a[t8]

v = a[t1]

19

i=i +1

20

a[t7] = t9

t2 = 4 * i

21

t3 = a[t2]

22

8
9
10
11

if t3 < v goto (5)


j=j1
t4 = 4 * j

23

t10 = 4 * j
a[t10] = x
goto (5)

24

t11 = 4 * i

25

x = a[t11]

26

t12 = 4 * i

27

t13 = 4 * n

12

t5 = a[t4]

13

if t5 > v goto (9)

28

t14 = a[t13]

14

if i >= j goto (23)

29

15

t6 = 4 * i

a[t12] = t14

30

t15 = 4 * n

11/11/16

x = a[t6]

Rajkumar S ,SCSE
Code optimization

a[t ] = x

35

Flow Graph

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 * i

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]

a[t7] = t9

a[t12] = t14

t10 = 4 * j

t15 = 4 * n

a[t10] = x

a[t15] = x

B2

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

goto B2

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

36

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 * i

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]

a[t7] = t9

a[t12] = t14

t10 = 4 * j

t15 = 4 * n

a[t10] = x

a[t15] = x

B2

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

goto B2

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

37

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 * i

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]

t10 = 4 * j

a[t12] = t14

a[t10] = x

t15 = 4 * n

goto B2

a[t15] = x

B2

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

38

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 *i

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]

a[t8] = x

a[t12] = t14

goto B2

t15 = 4 * n

B2

B3
j=j1
t4 = 4 * j

a[t15] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

39

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 * i

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]

a[t8] = x

a[t12] = t14

goto B2

t15 = 4 * n

B2

B3
j=j1
t4 = 4 * j

a[t15] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

40

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

t11 = 4 * i

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

a[t8] = x

t15 = 4 * n

goto B2

a[t15] = x

B2

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

41

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

B2
i=i +1

x = a[t6]

t2 = 4 * i

t8 = 4 * j

t3 = a[t2]

t9 = a[t8]

if t3 < v goto B2

a[t6] = t9

B3

a[t8] = x
j=j1

goto B2

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14
a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

42

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
t6 = 4 * i

B2
i=i +1

x = a[t6]

t2 = 4 * i

t8 = 4 * j

t3 = a[t2]

t9 = a[t8]

if t3 < v goto B2

a[t6] = t9

B3

a[t8] = x
j=j1

goto B2

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14
a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

43

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = a[t2]

B2
i=i +1

t8 = 4 * j

t2 = 4 * i

t9 = a[t8]

t3 = a[t2]

a[t2] = t9

if t3 < v goto B2

a[t8] = x

B3

goto B2
j=j1

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14
a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

44

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = t3

B2
i=i +1

t8 = 4 * j

t2 = 4 * i

t9 = a[t8]

t3 = a[t2]

a[t2] = t9

if t3 < v goto B2

a[t8] = x

B3

goto B2
j=j1

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14
a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

45

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = t3

B2
i=i +1

t9 = a[t4]

t2 = 4 * i

a[t2] = t9

t3 = a[t2]

a[t4] = x

if t3 < v goto B2

goto B2

B3

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14
a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

46

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = t3

B2
i=i +1

a[t2] = t5

t2 = 4 * i

a[t4] = x

t3 = a[t2]

goto B2

if t3 < v goto B2

t11 = 4 * i
x = a[t11]
t13 = 4 * n
t14 = a[t13]
a[t11] = t14

B3

a[t13] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

47

Common Subexpression Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = t3

B2
i=i +1

a[t2] = t5

t2 = 4 * i

a[t4] = x

t3 = a[t2]

goto B2

if t3 < v goto B2

B3

x = t3
t14 = a[t1]
a[t2] = t14
a[t1] = x

Similarly for B6

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

48

Dead Code Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
x = t3

B2
i=i +1

a[t2] = t5

t2 = 4 * i

a[t4] = x

t3 = a[t2]

goto B2

if t3 < v goto B2

x = t3
t14 = a[t1]
a[t2] = t14
a[t1] = x

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

49

Dead Code Elimination

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
a[t2] = t5

t14 = a[t1]

i=i +1

a[t4] = t3

a[t2] = t14

t2 = 4 * i

goto B2

a[t1] = t3

B2

t3 = a[t2]
if t3 < v goto B2

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

50

Reduction in Strength

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B5

B6
a[t2] = t5

t14 = a[t1]

i=i +1

a[t4] = t3

a[t2] = t14

t2 = 4 * i

goto B2

a[t1] = t3

B2

t3 = a[t2]
if t3 < v goto B2

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

51

Reduction in Strength

B1
i=m-1
j=n
t1 =4 * n
v = a[t1]

B2

t2 = 4 * i
t4 = 4 * j

B5

B6
a[t2] = t5

t14 = a[t1]

a[t4] = t3

a[t2] = t14

goto B2

a[t1] = t3

t2 = t 2 + 4
t3 = a[t2]

B3

if t3 < v goto B2

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

B4
if i >= j goto B6
11/11/16

Rajkumar S ,SCSE
Code optimization

52

Optimization of basic blocks

Substantial improvement can gained by performing local optimization


DAG Representation of basic blocks
A node for each initial values of the variables
Node for each statement
Children are the statements of the last definitions
Labeled by operators and variables for which it is the
last definition in the block
Some nodes are output nodes whose variables are live on exit

11/11/16

Rajkumar S ,SCSE
Code optimization

53

Basic Block

11/11/16

Rajkumar S ,SCSE
Code optimization

54

DAG for Basic Block

11/11/16

Rajkumar S ,SCSE
Code optimization

55

Optimimized Basic block

11/11/16

Rajkumar S ,SCSE
Code optimization

56

Dead Code Elimination

Delete root variables any that has no live variables, and


repeat the procedure

11/11/16

Rajkumar S ,SCSE
Code optimization

57

Dead Code Elimination

11/11/16

Rajkumar S ,SCSE
Code optimization

58

Use of Algebric Laws

11/11/16

Rajkumar S ,SCSE
Code optimization

59