Compilerconstructie najaar 2019 - - PowerPoint PPT Presentation

compilerconstructie
SMART_READER_LITE
LIVE PREVIEW

Compilerconstructie najaar 2019 - - PowerPoint PPT Presentation

Compilerconstructie najaar 2019 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 9, vrijdag 22 november 2019 + werkcollege Code Optimization (1) 1


slide-1
SLIDE 1

Compilerconstructie

najaar 2019 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 9, vrijdag 22 november 2019 + ‘werkcollege’ Code Optimization (1)

1

slide-2
SLIDE 2

8.5 Optimization of Basic Blocks

To improve running time of code

  • Local optimization: within block
  • Global optimization: across blocks

Local optimization benefits from DAG representation of basic block

2

slide-3
SLIDE 3

A slide from lecture 6:

6.2 Three-Address Code

  • Linearized representation of syntax tree / syntax DAG
  • Sequence of instructions: x = y op z

Example: a + a ∗ (b − c) + (b − c) ∗ d Syntax DAG

✟ ✟ ✟ ✟ ✟ ❍❍❍❍ ❍ ❅ ❅ ❅ ❅

+ + ∗ ∗ d a − b c

Three-address code t1 = b - c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4

3

slide-4
SLIDE 4

Example: x = a + a ∗ (b − c) + (b − c) ∗ d Syntax DAG

✟ ✟ ✟ ✟ ✟ ❍❍❍❍ ❍ ❅ ❅ ❅ ❅

❅ ✟ ✟ ✟ ✟ ✟ ❍❍❍❍ ❍ +

+ ∗ ∗ d a − b c = x

Three-address code t1 = b - c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4 x = t5

4

slide-5
SLIDE 5

Example: x = a + a ∗ (b − c) + (b − c) ∗ d DAG representation of basic block

✟ ✟ ✟ ✟ ✟ ❍❍❍❍ ❍ ❅ ❅ ❅ ❅

+ + ∗ ∗ d a − b c t5, x t3 t4 t2 t1

Three-address code t1 = b - c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4 x = t5

5

slide-6
SLIDE 6

8.5.1 DAG Representation of Basic Blocks

  • 1. A node for initial value of each variable appearing in block
  • 2. A node N for each statement s in block

Children of N are nodes corresponding to last definitions of

  • perands used by s
  • 3. Node N is labeled by operator applied at s

N has list of variables for which s is last definition in block

  • 4. Output nodes ≈ live on exit

Example: a = b + c b = a - d c = b + c d = a - d

6

slide-7
SLIDE 7

8.5.2 Finding Local Common Subexpressions

  • Use value-number method to detect common subexpressions
  • Remove redundant computations

Example: a = b + c b = a - d c = b + c d = a - d

  • DAG. . .

7

slide-8
SLIDE 8

Local Common Subexpression Elimination

  • Use value-number method to detect common subexpressions
  • Remove redundant computations

Example: a = b + c b = a - d c = b + c d = a - d a = b + c b = a - d c = b + c d = b

8

slide-9
SLIDE 9

Local Common Subexpression Elimination

  • Use value-number method to detect common subexpressions
  • Remove redundant computations

Example, if b is not live on exit: a = b + c b = a - d c = b + c d = a - d a = b + c d = a - d c = d + c

9

slide-10
SLIDE 10

Different assignments to same variable

d=a+b e=d+c d=b+c c=a+d

  • DAG. . .

reconstructing code. . .

10

slide-11
SLIDE 11

8.5.3 Dead Code Elimination

  • Remove roots with no live variables attached
  • If possible, repeat

Example: a = b + c b = b - d c = c + d e = b + c No common subexpression If c and e are not live. . .

11

slide-12
SLIDE 12

Dead Code Elimination

  • Remove roots with no live variables attached
  • If possible, repeat

Example: a = b + c b = b - d c = c + d e = b + c a = b + c b = b - d No common subexpression If c and e are not live. . .

12

slide-13
SLIDE 13

8.5.5 Representation of Array References

x = a[i] y = x+z z = a[i]

  • DAG. . .

13

slide-14
SLIDE 14

Representation of Array References

x = a[i] a[j] = y z = a[i]

  • DAG. . .

14

slide-15
SLIDE 15

Representation of Array References

b = 12 + a x = b[i] a[j] = y z = b[i] a[0] a[1] a[2] a[3] a[4] a b

❄ ❄

  • DAG. . .

15

slide-16
SLIDE 16

8.5.6 Pointer Assignments and Procedure Calls

a = b + c e = a - d c = b + c b = a - d

  • DAG. . .

16

slide-17
SLIDE 17

Pointer Assignments

  • vs. Common Subexpressions

p = &a a = b + c e = a - d *p = y c = b + c b = a - d

  • DAG. . .

17

slide-18
SLIDE 18

Pointer Assignments

  • vs. Common Subexpressions

a = b + c e = a - d *p = y c = b + c b = a - d

  • DAG. . .

18

slide-19
SLIDE 19

Pointer Assignments vs. Dead Code

a = b + c b = b - d c = c + d e = b + c x = *p

  • DAG. . .

If c and e are not live. . .

19

slide-20
SLIDE 20

To summarize: *q = y x = *p Procedure calls. . .

20

slide-21
SLIDE 21

8.5.4 The Use of Algebraic Identities

and other algebraic transformations (cf. assignment 3) Algebraic identities: x + 0 = 0 + x = x x ∗ 1 = 1 ∗ x = x Reduction in strength: x2 = x ∗ x (cheaper) 2 ∗ x = x + x (cheaper) x/2 = x ∗ 0.5 (cheaper) Constant folding: 2 ∗ 3.14 = 6.28

21

slide-22
SLIDE 22

Algebraic Transformations

Common subexpressions resulting from commutativity / asso- ciativity of operators: x ∗ y = y ∗ x c + d + b = (b + c) + d Common subexpressions generated by relational operators: x > y ⇔ x − y > 0

22

slide-23
SLIDE 23

8.5.7 Reassembling Basic Blocks From DAG’s

Order of instructions:

  • A. Order of instructions must respect order of nodes in DAG
  • B. Uses of same array may cross each other only if both are

array accesses

  • C. No statement may cross procedure call or assignment through

pointer

23

slide-24
SLIDE 24

8.5.7 Reassembling Basic Blocks From DAG’s

Order of instructions:

  • A. Order of instructions must respect order of nodes in DAG
  • B. Uses of same array may cross each other only if both are

array accesses

  • C. No statement may cross procedure call or assignment through

pointer

  • D. Assignments to same variable may not cross each other

24

slide-25
SLIDE 25

8.7 Peephole Optimization

  • Examines short sequence of instructions in a window (peep-

hole) and replace them by faster/shorter sequence

  • Applied to intermediate code or target code
  • Typical optimizations

– Redundant instruction elimination – Eliminating unreachable code – Flow-of-control optimization – Algebraic simplification – Use of machine idioms

25

slide-26
SLIDE 26

8.7.1 Eliminating Redundant Loads and Stores

Naive code generator may produce ST a, R0 LD R0, a N.B.: optimize only within basic block

26

slide-27
SLIDE 27

8.7.2 Eliminating Unreachable Code

Example: if debug == 1 goto L1 goto L2 L1: print debugging information L2: Jump over jump

27

slide-28
SLIDE 28

Eliminating Unreachable Code

Example: if debug != 1 goto L2 print debugging information L2: How to recognize that label L1 can be removed?

28

slide-29
SLIDE 29

Eliminating Unreachable Code

Example: if debug != 1 goto L2 print debugging information L2: If debug is set to 0 at beginning of program, . . .

29

slide-30
SLIDE 30

8.7.2 Eliminating Unreachable Code

Example: if debug == 1 goto L1 goto L2 L1: print debugging information L2: Even without jump over jump. . .

30

slide-31
SLIDE 31

8.7.3 Flow-of-Control Optimizations

Example 1: goto L1 ... L1: goto L2 Example 3: goto L1 . . . L1: if a < b goto L2 L3:

31

slide-32
SLIDE 32

8.7.3 Flow-of-Control Optimizations

Example 1: goto L1 goto L2 ... ... L1: goto L2 L1: goto L2 Example 3: goto L1 if a < b goto L2 . . . goto L3 L1: if a < b goto L2 ... L3: L3:

32

slide-33
SLIDE 33

9.1 The Principal Sources

  • f Optimization

Causes of redundancy

  • At source level
  • Side effect of high-level programming language, e.g., A[i][j]

33

slide-34
SLIDE 34

9.1.2 A Running Example: Quicksort

void quicksort (int m, int n) /* recursively sorts a[m] through a[n] */ { int i, j; int v, x; if (n <= m) return; 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; /* swap a[i], a[j] */ } x = a[i]; a[i] = a[n]; a[n] = x; /* swap a[i], a[n] */ quicksort(m,j); quicksort(i+1,n); }

34

slide-35
SLIDE 35

Three-Address Code Quicksort

− → (1) i = m-1 (2) j = n (3) t1 = 4*n (4) v = a[t1] − → (5) i = i+1 (6) t2 = 4*i (7) t3 = a[t2] (8) if t3<v goto (5) − → (9) j = j-1 (10) t4 = 4*j (11) t5 = a[t4] (12) if t5>v goto (9) − → (13) if i>=j goto (23) − → (14) t6 = 4*i (15) x = a[t6] (16) t7 = 4*i (17) t8 = 4*j (18) t9 = a[t8] (19) a[t7] = t9 (20) t10 = 4*j (21) a[t10] = x (22) goto (5) − → (23) t11 = 4*i (24) x = a[t11] (25) t12 = 4*i (26) t13 = 4*n (27) t14 = a[t13] (28) a[t12] = t14 (29) t15 = 4*n (30) a[t15] = x

35

slide-36
SLIDE 36

Flow Graph Quicksort

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t7] = t9 t10 = 4*j a[t10] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

36

slide-37
SLIDE 37

Local Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t7] = t9 t10 = 4*j a[t10] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

37

slide-38
SLIDE 38

Global Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t8 = 4*j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

38

slide-39
SLIDE 39

Global Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t9 = a[t4] a[t6] = t9 a[t4] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

39

slide-40
SLIDE 40

Global Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] a[t6] = t5 a[t4] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

40

slide-41
SLIDE 41

Global Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

x = a[t2] a[t2] = t5 a[t4] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

41

slide-42
SLIDE 42

Copy Propagation

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

x = t3 a[t2] = t5 a[t4] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

42

slide-43
SLIDE 43

Dead-Code Elimination

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

x = t3 a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

43

slide-44
SLIDE 44

Dead-Code Elimination

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

44

slide-45
SLIDE 45

Result So Far

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

45

slide-46
SLIDE 46

Local Common Subexpressions revisited

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t7 = 4*i t8 = 4*j t9 = a[t8] a[t7] = t9 t10 = 4*j a[t10] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

46

slide-47
SLIDE 47

Global Common Subexpressions

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

t6 = 4*i x = a[t6] t8 = 4*j t9 = a[t8] a[t6] = t9 a[t8] = x goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

47

slide-48
SLIDE 48

Code Motion

  • loop-invariant computation
  • compute before loop
  • Example:

while (i <= limit-2) /* statement does not change limit */ After code-motion t = limit-2 while (i <= t) /* statement does not change limit or t */

48

slide-49
SLIDE 49

Induction Variables and Reduction in Strength

  • Induction variable: each assignment to x of form x = x + c
  • Reduction in strength: replace expensive operation by cheaper
  • ne

49

slide-50
SLIDE 50

Induct.Var / Reduct.Strength

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

i = i+1 t2 = 4*i t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

50

slide-51
SLIDE 51

Induct.Var / Reduct.Strength

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

i = i+1 t2 = t2+4 t3 = a[t2] if t3 < v goto B2 B2

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if i >= j goto B6 B4

✟ ✟ ✟ ✟ ✙

a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

51

slide-52
SLIDE 52

Induct.Var / Reduct.Strength

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

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

✬ ✫ ✲ ❄

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

✬ ✫ ✲ ❄

if t2 >= t4 goto B6 B4

✟ ✟ ✟ ✟ ✙

a[t2] = t5 a[t4] = t3 goto B2 B5

✬ ✫ ✲ ❍❍❍❍ ❥

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

52

slide-53
SLIDE 53

Exercise 1

53

slide-54
SLIDE 54

Flow Graph Exercise 1

i = 0 t1 = 4*i min = a[t1] B1

t2 = n-1 if i < t2 goto B4 B2

✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❥

goto 21 B3

✟ ✟ ✟ ✟ ✙

i = i+1 t3 = 4*i t4 = a[t3] if t4 < min goto B6 B4

✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❥

goto B7 B5

✫ ✲

t5 = 4*0 t6 = 4*i t7 = a[t6] a[t5] = t7 t8 = 4*i a[t8] = min t9 = 4*0 min = a[t9] B6

goto B2 B7

✩ ✪ ✛

Constant folding Local common subexpression elim. Global common subexpression elim. Copy propagation Dead-code elimination Code motion Reduction in strength Induction-variable elimination

54

slide-55
SLIDE 55

Volgende week

  • Practicum over opdracht 4
  • Inleveren 12 december
  • Vrijdag 6 december: laatste hoor-/werkcollege

55

slide-56
SLIDE 56

Compilerconstructie

college 9 Code Optimization Chapters for reading: 8.5, 8.7 9.intro, 9.1

56