Download presentation

Presentation is loading. Please wait.

Published byRylee Sibley Modified over 2 years ago

1
Register Allocation CS 671 March 27, 2008

2
CS 671 – Spring 2008 1 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer instructions – most instrs are reg reg Cheaper instructions – accessing memory is expensive Option #1: Keep in Memory load r1, 4(sp) load r2, 8(sp) add r3,r1,r2 store r3, 12(sp) Option #2: Keep in Registers add r3,r1,r2

3
CS 671 – Spring 2008 2 Register Allocation – The Catch Very few registers available!! Some registers must be used for a particular purpose Integer vs. floating-point registers Doubleword registers (odd/even pairs) Branch and predicate registers Stack pointer R0 String manipulation (implicit uses)

4
CS 671 – Spring 2008 3 Register Allocation Register allocation – determines which of the values (variables, temporaries, large constants) should be in registers at each execution point Register assignment – determines which register should be used by each allocated value Goal - Want to minimize the traffic between the CPU registers and the memory hierarchy Probably the optimization with the most performance impact!

5
CS 671 – Spring 2008 4 A Simple Example Original code x 2 y 4 w x + y z x + 1 u x * y x z * 2 Symbolic register assignment s1 2 s2 4 s3 s1 + s2 s4 s1 + 1 s5 s1 * s2 s6 s4 * 2 Gen – variable is newly defined Kill – old value no longer accessible SSA – Static single assignment Def – on LHS Use – on RHS

6
CS 671 – Spring 2008 5 A Simple Example s1 2 s2 4 s3 s1 + s2 s4 s1 + 1 s5 s1 * s2 s6 s4 * 2 Live Range Interference S1S4 S3S2 S5 S6 Live – variable will be used again (before it is overwritten) Dead Variable will never be used again Value will be overwritten before next use s1 s2 s3 s4 s5 s6

7
CS 671 – Spring 2008 6 A Simple Example s1 2 s2 4 s3 s1 + s2 s4 s1 + 1 s5 s1 * s2 s6 s4 * 2 S1S4 S3S2 S5 S6 r1 2 r2 4 r3 r1 + r2 r3 r1 + 1 r1 r1 * r2 r2 r3 * 2 r1 green r2 blue r3 pink Graph Coloring Result

8
CS 671 – Spring 2008 7 Register Allocation Determine live ranges for each value (web) Determine overlapping ranges (interference) Compute the benefit of keeping each web in a register (spill cost) Decide which webs get a register (allocation) Split webs if needed (spilling and splitting) Assign hard registers to webs (assignment) Generate code including spills (code gen)

9
CS 671 – Spring 2008 8 Webs Starting Point: def-use chains (DU chains) Connects definition to all reachable uses Conditions for putting defs and uses into same web Def and all reachable uses must be in same web All defs that reach same use must be in same web

10
CS 671 – Spring 2008 9 Example def y def x use y def x def y use x def x use x use y s1 s2 s3 s4

11
CS 671 – Spring 2008 10 Interference Two webs interfere if their live ranges overlap (have a nonemtpy intersection) If two webs interfere, values must be stored in different registers or memory locations If two webs do not interfere, can store values in same register or memory location

12
CS 671 – Spring 2008 11 Example def y def x use y def x def y use x def x use x use y s1 s2 s3 s4 Webs s1 and s2 interfere Webs s2 and s3 interfere

13
CS 671 – Spring 2008 12 Interference Graph Representation of webs and their interference Nodes are the webs An edge exists between two nodes if they interfere s1s2 s3s4

14
CS 671 – Spring 2008 13 Example s1s2 s3s4 def y def x use y def x def y use x def x use x use y s1 s2 s3 s4 Webs s1 and s2 interfere Webs s2 and s3 interfere

15
CS 671 – Spring 2008 14 Reg Allocation Using Graph Coloring Each web is allocated a register each node gets a register (color) If two webs interfere they cannot use the same register if two nodes have an edge between them, they cannot have the same color s1s2 s3s4

16
CS 671 – Spring 2008 15 Graph Coloring Assign a color to each node in graph Two nodes connected to same edge must have different colors Classic problem in graph theory NP complete But good heuristics exist for register allocation

17
CS 671 – Spring 2008 16 Graph Coloring Example #1

18
CS 671 – Spring 2008 17 Graph Coloring Example #1 1 Color

19
CS 671 – Spring 2008 18 Graph Coloring Example #2

20
CS 671 – Spring 2008 19 Graph Coloring Example #2 2 Colors

21
CS 671 – Spring 2008 20 Graph Coloring Example #3

22
CS 671 – Spring 2008 21 Graph Coloring Example #3 Still 2 Colors

23
CS 671 – Spring 2008 22 Graph Coloring Example #4

24
CS 671 – Spring 2008 23 Graph Coloring Example #4 3 Colors

25
CS 671 – Spring 2008 24 Heuristics for Register Coloring Coloring a graph with N colors If degree < N (degree of a node = # of edges) Node can always be colored After coloring the rest of the nodes, you’ll have at least one color left to color the current node If degree >= N still may be colorable with N colors

26
CS 671 – Spring 2008 25 Heuristics for Register Coloring Remove nodes that have degree < N Push the removed nodes onto a stack When all the nodes have degree >= N Find a node to spill (no color for that node) Remove that node When empty, start to color Pop a node from stack back Assign it a color that is different from its connected nodes (since degree < N, a color should exist)

27
CS 671 – Spring 2008 26 Coloring Example #5 s1s2 s3s4 s0 N = 3

28
CS 671 – Spring 2008 27 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4

29
CS 671 – Spring 2008 28 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2

30
CS 671 – Spring 2008 29 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1

31
CS 671 – Spring 2008 30 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3

32
CS 671 – Spring 2008 31 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3 s0

33
CS 671 – Spring 2008 32 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3 s0

34
CS 671 – Spring 2008 33 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3

35
CS 671 – Spring 2008 34 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1

36
CS 671 – Spring 2008 35 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1

37
CS 671 – Spring 2008 36 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2

38
CS 671 – Spring 2008 37 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2

39
CS 671 – Spring 2008 38 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4

40
CS 671 – Spring 2008 39 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4

41
CS 671 – Spring 2008 40 Coloring Example #5 s1s2 s3s4 s0 N = 3

42
CS 671 – Spring 2008 41 Coloring Example #5 s1s2 s3s4 s0 N = 3

43
CS 671 – Spring 2008 42 Another Coloring Example s1s2 s3s4 s0 N = 3

44
CS 671 – Spring 2008 43 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

45
CS 671 – Spring 2008 44 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

46
CS 671 – Spring 2008 45 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

47
CS 671 – Spring 2008 46 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

48
CS 671 – Spring 2008 47 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2 s0

49
CS 671 – Spring 2008 48 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

50
CS 671 – Spring 2008 49 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

51
CS 671 – Spring 2008 50 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

52
CS 671 – Spring 2008 51 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

53
CS 671 – Spring 2008 52 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

54
CS 671 – Spring 2008 53 Another Coloring Example s1s2 s3s4 s0 N = 3

55
CS 671 – Spring 2008 54 Another Coloring Example s1s2 s3s4 s0 N = 3

56
CS 671 – Spring 2008 55 When Coloring Fails? Option 1 Pick a web and allocate value in memory All defs go to memory, all uses come from memory Option 2 Split the web into multiple webs In either case, will retry the coloring

57
CS 671 – Spring 2008 56 Which web to choose? One with interference degree >= N One with minimal spill cost (cost of placing value in memory rather than in register) What is spill cost? Cost of extra load and store instructions

58
CS 671 – Spring 2008 57 Ideal and Useful Spill Costs Ideal spill cost - dynamic cost of extra load and store instructions. Can’t expect to compute this. Don’t know which way branches resolve Don’t know how many times loops execute Actual cost may be different for different executions Solution: Use a static approximation profiling can give instruction execution frequencies or use heuristics based on structure of control flow graph

59
CS 671 – Spring 2008 58 One Way to Compute Spill Cost Goal: give priority to values used in loops So assume loops execute 8 or 10 times Spill cost = sum over all def sites: cost of a store instruction times 10 to the loop nesting depth power, plus sum over all use sites: cost of a load instruction times 10 to the loop nesting depth power Choose the web with the lowest spill cost

60
CS 671 – Spring 2008 59 Spill Cost Example def x def y use y def y use x use y Spill Cost For x storeCost + loadCost Spill Cost For y 10*storeCost + 10*loadCost With 1 Register, Which Variable Gets Spilled?

61
CS 671 – Spring 2008 60 Outline What is register allocation A simple register allocator Webs Interference Graphs Graph coloring Splitting More optimizations

62
CS 671 – Spring 2008 61 Splitting Rather Than Spilling Split the web Split a web into multiple webs so that there will be less interference in the interference graph making it N-colorable Spill the value to memory and load it back at the points where the web is split

63
CS 671 – Spring 2008 62 Splitting Example def z use z def x def y use x use y use z x y z

64
CS 671 – Spring 2008 63 Splitting Example def z use z def x def y use x use y use z x y z xy z

65
CS 671 – Spring 2008 64 Splitting Example def z use z def x def y use x use y use z x y z xy z 2 colorable?

66
CS 671 – Spring 2008 65 Splitting Example def z use z def x def y use x use y use z x y z xy z 2 colorable? NO!

67
CS 671 – Spring 2008 66 Splitting Example def z use z def x def y use x use y use z x y z

68
CS 671 – Spring 2008 67 Splitting Example def z use z def x def y use x use y use z x y z

69
CS 671 – Spring 2008 68 Splitting Example def z use z def x def y use x use y use z x y z xy z2 z1

70
CS 671 – Spring 2008 69 Splitting Example def z use z def x def y use x use y use z x y z xy z2 z1 2 colorable?

71
CS 671 – Spring 2008 70 Splitting Example def z use z def x def y use x use y use z x y z xy z2 z1 2 colorable? YES!

72
CS 671 – Spring 2008 71 Splitting Example def z use z def x def y use x use y use z x y z r1 r2 r1 xy z2 z1 2 colorable? YES!

73
CS 671 – Spring 2008 72 Splitting Example def z use z str z def x def y use x use y ld z use z x y z r1 r2 r1 xy z2 z1 2 colorable? YES!

74
CS 671 – Spring 2008 73 Splitting Heuristic Identify a program point where the graph is not R-colorable (point where # of webs > N) Pick a web that is not used for the largest enclosing block around that point of the program Split that web at the corresponding edge Redo the interference graph Try to re-color the graph

75
CS 671 – Spring 2008 74 Cost and benefit of splitting Cost of splitting a node Proportional to number of times split edge has to be crossed dynamically Estimate by its loop nesting Benefit Increase colorability of the nodes the split web interferes with Can approximate by its degree in the interference graph Greedy heuristic pick the live-range with the highest benefit-to- cost ratio to spill

76
CS 671 – Spring 2008 75 Outline What is register allocation A simple register allocator Webs Interference Graphs Graph coloring Splitting More optimizations

77
CS 671 – Spring 2008 76 Further Optimizations Register coalescing Register targeting (pre-coloring) Presplitting of webs Interprocedural register allocation

78
CS 671 – Spring 2008 77 Register Coalescing Find register copy instructions sj = si If sj and si do not interfere, combine their webs Pros reduce the number of instructions Cons may increase the degree of the combined node a colorable graph may become non-colorable

79
CS 671 – Spring 2008 78 Register Targeting (pre-coloring) Some variables need to be in special registers at a given time first 4 arguments to a function return value Pre-color those webs and bind them to the correct register Will eliminate unnecessary copy instructions

80
CS 671 – Spring 2008 79 Pre-splitting of the webs Some live ranges have very large “dead” regions. Large region where the variable is unused Break-up the live ranges need to pay a small cost in spilling but the graph will be very easy to color Can find strategic locations to break-up at a call site (need to spill anyway) around a large loop nest (reserve registers for values used in the loop)

81
CS 671 – Spring 2008 80 Interprocedural register allocation Saving/restoring registers across procedure boundaries is expensive especially for programs with many small functions Calling convention is too general and inefficient Customize calling convention per function by doing interprocedural register allocation

82
CS 671 – Spring 2008 81 Summary Register Allocation Store values in registers between def and use Can improve performance substantially Key concepts Webs Interference graphs Colorability Splitting

Similar presentations

OK

Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.

Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on circuit switching network Download ppt on sectors of economy Ppt on ganga river pollution Ppt on water scarcity in india Ppt on rainwater harvesting in india download Ppt on column chromatography Ppt on omission of articles in spanish Ppt on earth hour toronto Ppt on power generation using footsteps clothing Ppt on android os software