Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall Compiler Principles Lecture 13: Summary

Similar presentations


Presentation on theme: "Fall Compiler Principles Lecture 13: Summary"— Presentation transcript:

1 Fall 2014-2015 Compiler Principles Lecture 13: Summary
Roman Manevich Ben-Gurion University

2 Syllabus mid-term exam Front End Intermediate Representation
Scanning Top-down Parsing (LL) Bottom-up Parsing (LR) Intermediate Representation Lowering Optimizations Local Optimizations Dataflow Analysis Loop Optimizations Code Generation Register Allocation mid-term exam

3 Previously Global register allocation
By Graph coloring – Chaitin’s algorithm Linear scan

4 agenda Clarify Chaitin’s algorithm Rehearse register allocation

5 Conservative coalescing

6 RIG with MOVE edges Add a second type of edges – MOVE edges
color register eax ebx a b c d e

7 Conservative coalescing
Coalesce MOVE edge (a,b) if it does not affect colorability A node is heavy if its degree is ≥K and light otherwise Briggs criterion: coalesce only if the merged node ab has <K nodes heavy nodes Reason: Simplify will first remove all light neighbors ab will then be adjacent to <K neighbors Simplify can remove ab

8 Conservative coalescing
Coalesce MOVE edge (a,b) if it does not affect colorability A node is heavy if its degree is ≥K and light otherwise George criterion: coalesce only if all heavy neighbors of a are also neighbors of b Reason: Simplify can remove all light neighbors of a Remaining heavy neighbors of a are neighbors of b so coalescing does not increase the degree of any node

9 Coalescing criterion example
By the Briggs criterion? #(heavy-neighbors(a,e) < K By the George criterion? All heavy neighbors of a are also neighbors of e a Can we coalesce (a,e) ? color register b c eax e ebx

10 Coalescing criterion example
By the Briggs criterion? NO #(heavy-neighbors(a,e) < K By the George criterion? YES All heavy neighbors of a are also neighbors of e a Can we coalesce (a,e) ? color register b c eax e ebx

11 Overall algorithm

12 Simplify, coalesce and freeze
Phase 1: Simplify Step 1 (simplify): simplify as much as possible without removing nodes that are the source or destination of a move (MOVE-related nodes) Step 2 (coalesce): coalesce a MOVE-related edge provided low-degree node results Step 3 (freeze): if neither steps 1 or 2 apply, freeze a MOVE instruction: low-degree nodes involved are marked not MOVE-related (remove MOVE edge) and try step 1 again Step 4 (spill): if all nodes are heavy select a candidate for spilling using a priority function and move to potential spill stack Phase 2: Select Unwind stack and reconstruct the graph as follows: Pop variable from the stack Add it back to the graph Color the node node with a color that it doesn’t interfere with Unwind potential spill stack and attempt to color node – if unable mark corresponding variable as actual spill Phase 4: Rewrite Allocate position in frame for spilled variable v On each usage of v load to vi from frame

13 Simplify, coalesce and freeze
Simplify: recursively remove non-MOVE nodes with <K neighbors, pushing them onto stack any simplify done Coalesce: conservatively merge unconstrained MOVE-related nodes with <K “heavy” neighbors any coalesce done Freeze: give up coalescing on some low-degree MOVE-related node any freeze done

14 Overall algorithm Simplify, freeze and coalesce Liveness analysis
any potential spill done Mark potential spills Select colors and detect actual spills Rewrite code to implement actual spills any actual spill done

15 Potential spill example

16 Potential spill example
Color the following graph d a color register potential spill: (color) stack: AX BX e b CX f c

17 Potential spill example
Pick a for potential spill d a color register potential spill: (color) stack: AX BX e b CX f c

18 Potential spill example
Simplify d d a color register potential spill: a (color) stack: AX BX e b CX f c

19 Potential spill example
Simplify e d a color register potential spill: a (color) stack: d AX BX e b CX f c

20 Potential spill example
Simplify b d a color register potential spill: a (color) stack: e d AX BX e b CX f c

21 Potential spill example
Simplify c d a color register potential spill: a (color) stack: b e d AX BX e b CX f c

22 Potential spill example
Simplify f d a color register potential spill: a (color) stack: c b e d AX BX e b CX f c

23 Potential spill example
Select color for f d a color register potential spill: a (color) stack: f c b e d AX BX e b CX f c

24 Potential spill example
Select color for c d a color register potential spill: a (color) stack: c b e d AX BX e b CX f c

25 Potential spill example
Select color for b d a color register potential spill: a (color) stack: b e d AX BX e b CX f c

26 Potential spill example
Select color for e d a color register potential spill: a (color) stack: e d AX BX e b CX f c

27 Potential spill example
Select color for d d a color register potential spill: a (color) stack: d AX BX e b CX f c

28 Potential spill example
Select color for a d a color register potential spill: a (color) stack: AX BX e b CX f c

29 Potential spill example
Done – no actual spill d a color register potential spill: (color) stack: AX BX e b CX f c

30 actual spill example

31 Actual spill example Apply Chaitin’s algorithm for the following program The set of registers is AX, BX, CX Use the Briggs criterion for conservative coalescing Upon any non-deterministic choice, choose by lexicographic order c := 2; a := a + e; d := a + c; d := d + b; d := d + b; // live = {d}

32 Step 1: compute liveness
Let’s compute liveness information {a, b, e} c := 2; {a, b, c, e} a := a + e; {a, b, c} d := a + c; {b, d} d := d + b; {b, d} d := d + b; // live = {d}

33 Step 2: draw RIG Simplify d color register AX BX CX a potential spill:
(color) stack: CX b c d e

34 Potentially spill b #use+def(a)=3, #use+def(b)=2, #use+def(c)=2, #use+def(d)=3 (potentially) spill b c := 2; a := a + e; d := a + c; d := d + b; d := d + b; // live = {d} color register AX a BX potential spill: (color) stack: d CX b c d e

35 Simplify Simplify a color register AX BX CX a potential spill: b
(color) stack: d CX b c d e

36 Simplify Simplify c color register AX BX CX a potential spill: b
(color) stack: a d CX b c d e

37 Simplify Simplify e color register AX BX CX a potential spill: b
(color) stack: c a d CX b c d e

38 Attempt to color nodes on stack
Select color for e color register AX a BX potential spill: b (color) stack: e c a d CX b c d e

39 Attempt to color nodes on stack
Select color for c color register AX a BX potential spill: b (color) stack: c a d CX b c d e

40 Attempt to color nodes on stack
Select color for a color register AX a BX potential spill: b (color) stack: a d CX b c d e

41 Attempt to color nodes on stack
Select color for d color register AX a BX potential spill: b (color) stack: CX b c d e

42 b cannot be colored Cannot color b – actual spill color register AX BX
potential spill: b (color) stack: CX b c d e

43 Actual spill: rich assembly
Suppose assembly allows commands of the form x:=y+M[offset] Rewrite program for spilled variable b Choose location on frame: b_loc c := 2; a := a + e; d := a + c; d := d + b; d := d + b; // live = {d} c := 2; a := a + e; d := a + c; d := d + M[b_loc]; d := d + M[b_loc]; // live = {d}

44 Actual spill: basic assembly
Rewrite program for spilled variable b Choose location on frame: b_loc Use temporaries for reading from frame: b1, b2 Now attempt to color all variables, including temporaries If unable don’t spill temporaries, choose other variables to spill, otherwise can go into infinite spill-color loop c := 2; a := a + e; d := a + c; d := d + b; d := d + b; // live = {d} c := 2; a := a + e; d := a + c; b1 := M[b_loc]; d := d + b1; b2 := M[b_loc]; d := d + b2; // live = {d}

45 Compute liveness for new program
{a, e} c := 2; {a, c, e} a := a + e; {a, c} d := a + c; {d} b1 := M[b_loc]; {d, b1} d := d + b1; {d} b2 := M[b_loc]; {d, b2} d := d + b2; // live = {d}

46 Attempt to color Simplify b1, b2, d, a, c, e Select colors
color register AX b1 a BX CX b2 c d e

47 Attempt to color Simplify b1, b2, d, a, c, e Select colors
color register AX b1 a BX CX b2 c d e

48 Emit code based on colors
c := 2; a := a + e; d := a + c; b1 := M[b_loc]; d := d + b1; b2 := M[b_loc]; d := d + b2; color register AX b1 a BX BX := 2; CX := CX + AX; AX := CX + BX; BX := M[b_loc]; AX := AX + BX; BX := M[b_loc]; AX := AX + BX; CX b2 c d e

49 Coalesce example

50 Coalesce example Apply Chaitin’s algorithm for the following program
The set of registers is AX, BX, CX Use the Briggs criterion for conservative coalescing Upon any non-deterministic choice, choose by lexicographic order a := e; d := a + c; d := d + b; // live = {d}

51 Step 1: compute liveness
{b, c, e} a := e; {a, b, c} d := a + c; {b, d} d := d + b; // live = {d}

52 Attempt to color Simplify d color register AX BX CX a potential spill:
(color) stack: CX b c d e

53 Coalesce step Cannot simplify a or e because they are MOVE-related
Coalesce (a,e) color register AX a BX potential spill: (color) stack: d CX b c d e

54 Simplify Simplify ae color register AX BX CX ae potential spill:
(color) stack: d CX b c d

55 Simplify Simplify b color register AX BX CX ae potential spill:
(color) stack: ae d CX b c d

56 Simplify Simplify c color register AX BX CX ae potential spill:
(color) stack: b ae d CX b c d

57 Select Color c color register AX BX CX ae potential spill:
(color) stack: c b ae d CX b c d

58 Select Color b color register AX BX CX ae potential spill:
(color) stack: b ae d CX b c d

59 Select Color ae color register AX BX CX ae potential spill:
(color) stack: ae d CX b c d

60 Select Color d color register AX BX CX ae potential spill:
(color) stack: d CX b c d

61 Select color register AX BX CX ae potential spill: (color) stack: b c
d

62 Emit code CX := CX; AX := CX + AX; AX := AX + BX;
a := e; d := a + c; d := d + b; CX := CX; AX := CX + AX; AX := AX + BX; color register AX ae BX CX b c d

63 Freeze example

64 Step 1: compute liveness
{b, c, e} a := e; {a, b, c} d := a + c; {b, d} d := d + b; // live = {d}

65 Attempt to color Simplify d color register AX BX a potential spill:
(color) stack: b c d e

66 Attempt to color Can we coalesce a and e according to Briggs?
Let’s try color register AX a BX potential spill: (color) stack: d b c e

67 Unsuccessful attempt to color
No, node becomes heavy Undo coalesce and try something else color register AX a&e BX potential spill: (color) stack: d b c

68 Freeze: simplify a color register AX BX a potential spill:
(color) stack: d b c e

69 Simplify c color register AX BX potential spill: (color) stack: a d b

70 Simplify b color register AX BX potential spill: (color) stack: c a d

71 Simplify e color register AX BX potential spill:
(color) stack: b c a d e

72 Select color for e color register AX BX potential spill:
(color) stack: e b c a d

73 Select color for b color register AX BX potential spill:
(color) stack: b c a d e

74 Select color for c color register AX BX potential spill:
(color) stack: c a d b e

75 Select color for a color register AX BX potential spill:
(color) stack: a d b c e

76 Select color for d color register AX BX a potential spill:
(color) stack: d b c e

77 Select color for d color register AX BX a potential spill:
(color) stack: b c d e

78 Linear scan allocation practice

79 Exercise Draw the live intervals for the following program
Indicate which MOVE statements may be eliminated by register coalescing Explain what is the general (sufficient) condition

80 Linear scan with coalescing
b c d e f g e := d + a b := b + c f := b IfZ e Goto _L d := e + f d := e - f Goto _L1; _L0: d := e-f _L1: g := d

81 Linear scan with coalescing
b c d e f g e := d + a b := b + c f := b IfZ e Goto _L d := e + f d := e - f Goto _L1; _L0: d := e-f _L1: g := d

82 Linear scan with coalescing
b c d e f g x:=y can be coalesced if Interval(y) ends where Interval(x) begins x is not spilled e := d + a b := b + c f := b IfZ e Goto _L d := e + f d := e - f Goto _L1; _L0: d := e-f _L1: g := d

83 Good luck with final exam and project
Advertisement for next semester: Program Analysis and Verification Mini-project on Implementing Parallel Graph Algorithms


Download ppt "Fall Compiler Principles Lecture 13: Summary"

Similar presentations


Ads by Google