Download presentation

Presentation is loading. Please wait.

Published byElissa Alexander Modified over 2 years ago

1
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett

2
Last Time: Dataflow analysis on CFG's Find iterative solution to equations: –Available Expressions (forwards): Din[L] = Dout[L 1 ] … Dout[L n ] where pred[L] = {L 1,…,L n } Dout[L] = (Din[L] - Kill[L]) Gen[L] –live variable sets (backwards): LiveIn[L] = Gen[L] (LiveOut[L] - Kill[L]) LiveOut[L] = LiveIn[L 1 ] … LiveIn[L n ] where succ[L] = {L 1,…,L n }

3
Register Allocation Goal is to assign each temp to one of k registers. (The general problem is NP-complete.) So we use a heuristic that turns out to be poly-time for the particular situation we are in. Build interference graph G –G(x,y)=true if x & y are live at same point. Simplify the graph G –If x has degree < k, push x and simplify G-{x} –if no such x, then we need to spill some temp. Once graph is empty, start popping temps and assigning them registers. –Always have a free register since sub-graph G-{x} can't have >= k interfering temps.

4
Example from book {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k} j k h g d c b m f e

5
Simplification (4 regs) Stack: j k h g d c b m f e

6
Simplification Stack: g j k h g d c b m f e

7
Simplification Stack: g j k h d c b m f e

8
Simplification Stack: g h j k h d c b m f e

9
Simplification Stack: g h j k d c b m f e

10
Simplification Stack: g h k j k d c b m f e

11
Simplification Stack: g h k j d c b m f e

12
Simplification Stack: g h k d j d c b m f e

13
Simplification Stack: g h k d j c b m f e

14
Simplification Stack: g h k d j j c b m f e

15
Simplification Stack: g h k d j e c b m f e

16
Simplification Stack: g h k d j e f c b m f

17
Simplification Stack: g h k d j e f b c b m

18
Simplification Stack: g h k d j e f b c c m

19
Simplification Stack: g h k d j e f b c m

20
Select: Stack: g h k d j e f b c m j k h g d c b m f e

21
Select: Stack: g h k d j e f b c j k h g d c b m f e

22
Select: Stack: g h k d j e f b j k h g d c b m f e

23
Select: Stack: g h k d j e f j k h g d c b m f e

24
Select: Stack: g h k d j e j k h g d c b m f e

25
Select: Stack: g h k d j j k h g d c b m f e

26
Select: Stack: g h k d j k h g d c b m f e

27
Select: Stack: g h k j k h g d c b m f e

28
Select: Stack: g h j k h g d c b m f e

29
Select: Stack: g j k h g d c b m f e

30
Use coloring to codegen: g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b j k h g d c b m f e t1 t2 t3 t4

31
Use coloring to codegen: t2 := *(t4+12) t1 := t1 - 1 t2 := t2 * t1 t3 := *(t4+8) t1 := *(t4+16) t2 := *(t2+0) t3 := t3 + 8 t3 := t3 t1 := t1 + 4 t4 := t2 j k h g d c b m f e Notice that we can simplify away moves such as this one…

32
Spilling… Suppose all of the nodes in the graph have degree >= k. Pick one of the nodes to spill. –Picking a high-degree temp will make it more likely that we can color the rest of the graph. –Picking a temp that is used infrequently will likely generate better code. e.g., spilling a register used in a loop when we could spill one accessed outside the loop is a bad idea… Rewrite the code: –after definition of temp, write it into memory. –before use of temp, load it into another temp.

33
Try it with 3 registers… {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k} j k h g d c b m f e

34
Simplify: Stack: h j k h g d c b m f e

35
Simplify: Stack: h c j k g d c b m f e

36
Simplify: Stack: h c g j k g d b m f e

37
Simplify: Stack: h c g j k d b m f e

38
3 Regs Stack: h c g j k d b m f e We're stuck…

39
3 Regs j k d b m f e Don't want to spill j, it's used a lot. Don't want to spill f or k, they have relatively low degree. So let's pick m… {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k}

40
Rewrite: {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) *(fp+ ) := m b := *(f+0) c := e + 8 d := c m2 := *(fp+ ) k := m2 + 4 j := b {live-out: d,j,k} Eliminated this chunk of code from m's live range…

41
New Interference Graph j k h g d c b m f e {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) *(fp+ ) := m b := *(f+0) c := e + 8 d := c m2 := *(fp+ ) k := m2 + 4 j := b {live-out: d,j,k} m2

42
Simplify: j k h g d c b m f e Stack: m2 c m2

43
Simplify: j k h g d b m f e Stack: m2 c h

44
Back to simplification… Stack: m2 c h m j k g d b m f e

45
Back to simplification… Stack: m2 c h m f j k g d b f e

46
Back to simplification… Stack: m2 c h m f g j k g d b e

47
Back to simplification… Stack: m2 c h m f g e j k d b e

48
Back to simplification… Stack: m2 c h m f g e j j k d b

49
Back to simplification… Stack: m2 c h m f g e j k k d b

50
Back to simplification… Stack: m2 c h m f g e j k d d b

51
Back to simplification… Stack: m2 c h m f g e j k d b b

52
Then Color Stack: m2 c h m f g e j k d b j k h g d c b m f e m2

53
Then Color Stack: m2 c h m f g e j k d j k h g d c b m f e m2

54
Then Color Stack: m2 c h m f g e j k j k h g d c b m f e m2

55
Then Color Stack: m2 c h m f g e j j k h g d c b m f e m2

56
Then Color Stack: m2 c h m f g e j k h g d c b m f e m2

57
Then Color Stack: m2 c h m f g j k h g d c b m f e m2

58
Then Color Stack: m2 c h m f j k h g d c b m f e m2

59
Then Color Stack: m2 c h m j k h g d c b m f e m2

60
Then Color Stack: m2 c h j k h g d c b m f e m2

61
Then Color Stack: m2 c j k h g d c b m f e m2

62
Then Color Stack: m2 j k h g d c b m f e

63
Register Pressure Some optimizations increase live-ranges: –Copy propagation –Common sub-expression elimination –Loop invariant removal In turn, that can cause the allocator to spill. Copy propagation isn't that useful anyway: –Let register allocator figure out if it can assign the same register to two temps! –Then the copy can go away. –And we don't have to worry about register pressure.

64
Next Time: How to do coalescing register allocation. An optimistic spilling strategy. Some real-world issues: –caller/callee-saves registers –fixed resources (e.g., mflo, mfhi) –allocation of stack slots

Similar presentations

OK

Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}

Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

English 8 unit 12 read ppt on iphone Download ppt on global warming and greenhouse effect Ppt on two point perspective letters Ppt on balancing redox reactions Ppt on pi in maths lesson Download free ppt on job satisfaction Download ppt on strings in c language Mp ppt online form 2016 Ppt on power diode application Ppt on mercury and venus