Docsity

OOPS - Code Optimizations, Study notes of Object Oriented Programming

Partial preview of the text

Download OOPS - Code Optimizations and more Study notes Object Oriented Programming in PDF only on Docsity!

Code Optimization

Introduction

Criteria for Code-Improving Transformation:

  • (^) Meaning must be preserved (correctness)
  • (^) Speedup must occur on average.
  • (^) Work done must be worth the effort.

Peephole Optimizations

Constant Folding

x := 32 becomes x := 64

x := x + 32

Unreachable Code

goto L

x := x + 1 ๏ƒŸ No need

Flow of control optimizations

goto L1 becomes goto L

L1: goto L2 ๏ƒŸ No needed if no other L1 branch

Peephole Optimizations

Algebraic Simplification

x := x + 0 ๏ƒŸ No needed

Dead code

x := 32 ๏ƒŸ where x not used after statement

y := y + 32

Reduction in strength

x := x * 2 ๏ƒ  x := x + x

๏ƒ  x := x << 2

Three Address Code of Quick Sort

i = m - 1

j = n

t 1 =4 * n

v = a[t 1 ]

i = i + 1

t 2 = 4 * i

t 3 = a[t 2 ]

if t 3 < v goto (5)

j = j โ€“ 1

t 4 = 4 * j

t 5 = a[t 4 ]

if t 5 > v goto (9)

if i >= j goto (23)

t 6 = 4 * i

x = a[t 6 ]

t 7 = 4 * I

t 8 = 4 * j

t 9 = a[t 8 ]

a[t 7 ] = t 9

t 10 = 4 * j

a[t 10 ] = x

goto (5)

t 11 = 4 * I

x = a[t 11 ]

t 12 = 4 * i

t 13 = 4 * n

t 14 = a[t 13 ]

a[t 12 ] = t 14

t 15 = 4 * n

a[t ] = x

Find The Basic Block

i = m - 1

j = n

t 1 =4 * n

v = a[t 1 ]

i = i + 1

t 2 = 4 * i

t 3 = a[t 2 ]

if t 3 < v goto (5)

j = j โ€“ 1

t 4 = 4 * j

t 5 = a[t 4 ]

if t 5 > v goto (9)

if i >= j goto (23)

t 6 = 4 * i

x = a[t 6 ]

t 7 = 4 * I

t 8 = 4 * j

t 9 = a[t 8 ]

a[t 7 ] = t 9

t 10 = 4 * j

a[t 10 ] = x

goto (5)

t 11 = 4 * i

x = a[t 11 ]

t 12 = 4 * i

t 13 = 4 * n

t 14 = a[t 13 ]

a[t 12 ] = t 14

t 15 = 4 * n

a[t ] = x

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

t 6 = 4 * i

x = a[t 6 ]

t 7 = 4 * i t 8 = 4 * j t 9 = a[t 8 ] a[t 7 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

t 6 = 4 * i

x = a[t 6 ]

t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

t 6 = 4 * i

x = a[t 6 ]

t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

t 6 = 4 * i

x = a[t 6 ]

t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 t 15 = 4 * n a[t 15 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

t 6 = 4 * i

x = a[t 6 ]

t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

x = a[ t 2 ]

t 8 = 4 * j t 9 = a[t 8 ] a[ t 2 ] = t 9 a[ t 8 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

x = t 3 t 9 = a[ t 4 ] a[ t 2 ] = t 9 a[ t 4 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x

B 1

B 2

B 3

B 4

B 5 B 6

Common Subexpression Elimination

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

i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2

j = j โ€“ 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3

if i >= j goto B 6

x = t 3 a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2

t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x

B 1

B 2

B 3

B 4

B 5 B 6