Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.

Similar presentations


Presentation on theme: "Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation."— Presentation transcript:

1 Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation

2 CS243 Winter 2006 Stanford University 2 Register Allocation Introduction Introduction Problem Formulation Problem Formulation Algorithm Algorithm

3 CS243 Winter 2006 Stanford University 3 Register Allocation Goal Allocation of variables (pseudo-registers) in a procedure to hardware registers Allocation of variables (pseudo-registers) in a procedure to hardware registers Directly reduces running time by converting memory access to register access Directly reduces running time by converting memory access to register access What’s memory latency in CPU cycles? What’s memory latency in CPU cycles?

4 CS243 Winter 2006 Stanford University 4 Example a = b + c How long will the loop take, if a, b, and c are all in memory?

5 CS243 Winter 2006 Stanford University 5 Example Ld r1, b Ld r2, c Add r0, r1, r2 St a, r0 a = b + c

6 CS243 Winter 2006 Stanford University 6 Example Ld r1, b Ld r2, c Add r0, r1, r2 St a, r0 How long will the loop take?

7 CS243 Winter 2006 Stanford University 7 Revisit SSA Example Mapping a i to a is like register allocation Mapping a i to a is like register allocation a 1 = b 1 a 1 +5 b2=b2= a 1 = b 1 b 1 +5 b2=b2= a 1 = b 1 b 1 +5 b2=b2= a 1 +5

8 CS243 Winter 2006 Stanford University 8 High-level Steps Find an assignment for all pseudo- registers or alias-free variables. Find an assignment for all pseudo- registers or alias-free variables. Assign a hardware register for each variable. Assign a hardware register for each variable. If there are not enough registers in the machine, choose registers to spill to memory If there are not enough registers in the machine, choose registers to spill to memory

9 CS243 Winter 2006 Stanford University 9 Register Allocation Introduction Introduction Problem Formulation Problem Formulation Algorithm Algorithm

10 CS243 Winter 2006 Stanford University 10 Problem Formulation Two pseudo-registers interfere if at some point in the program they can not both occupy the same register. Two pseudo-registers interfere if at some point in the program they can not both occupy the same register. Interfere Graph: Interfere Graph: nodes = pseudo-registers nodes = pseudo-registers There is an edge between two nodes if their corresponding pseudo-registers interfere There is an edge between two nodes if their corresponding pseudo-registers interfere

11 CS243 Winter 2006 Stanford University 11 Pseudo-registers a = 1 b = 2 c = a + b d = a + 3 e = a + b t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2

12 CS243 Winter 2006 Stanford University 12 Interference Graph t1 t2 t3 t4 t5

13 CS243 Winter 2006 Stanford University 13 Graph Coloring A graph is n-colorable, if every node is the graph can be colored with one of the n colors such that two adjacent nodes do not have the same color. A graph is n-colorable, if every node is the graph can be colored with one of the n colors such that two adjacent nodes do not have the same color. To determine if a graph is n-colorable is NP-complete, for n>2 To determine if a graph is n-colorable is NP-complete, for n>2 Too expensive Too expensive Heuristics Heuristics

14 CS243 Winter 2006 Stanford University 14 Example How many colors are needed? How many colors are needed?

15 CS243 Winter 2006 Stanford University 15 Graph Coloring and Register Allocation Assigning n registers (without spilling) = Coloring with n colors Assigning n registers (without spilling) = Coloring with n colors Assign a node to a register (color) such that no two adjacent nodes are assigned same registers (colors) Assign a node to a register (color) such that no two adjacent nodes are assigned same registers (colors) Is spilling necessary? = Is the graph n- colorable? Is spilling necessary? = Is the graph n- colorable?

16 CS243 Winter 2006 Stanford University 16 Register Allocation Introduction Introduction Problem Formulation Problem Formulation Algorithm Algorithm

17 CS243 Winter 2006 Stanford University 17 Algorithm Step 1: Build an interference graph Step 1: Build an interference graph Refining the notion of a node Refining the notion of a node Finding the edges Finding the edges Step 2: Coloring Step 2: Coloring Use heuristics to find an n-coloring Use heuristics to find an n-coloring Successful → colorable and we have an assignment Successful → colorable and we have an assignment Failure → graph not colorable, or graph is colorable, but it is too expensive to color Failure → graph not colorable, or graph is colorable, but it is too expensive to color

18 CS243 Winter 2006 Stanford University 18 Step 1a: Nodes in Interference Graph t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 Every pseudo-register is a node

19 CS243 Winter 2006 Stanford University 19 Step 1b: Edges of Interference Graph Intuition Intuition Two live ranges (necessarily different variables) may interfere if they overlap at some point in the program Two live ranges (necessarily different variables) may interfere if they overlap at some point in the program

20 CS243 Winter 2006 Stanford University 20 Live Ranges Motivation: to create an interference graph that is easier to color Motivation: to create an interference graph that is easier to color Eliminate interference in a variable’s “dead” zones Eliminate interference in a variable’s “dead” zones Increase flexibility in allocation: can allocation same variable to different registers Increase flexibility in allocation: can allocation same variable to different registers A live range consists of a definition and all the points in a program (e.g. end of an instruction) in which that definition is live. A live range consists of a definition and all the points in a program (e.g. end of an instruction) in which that definition is live.

21 CS243 Winter 2006 Stanford University 21 Merged Live Ranges Two overlapping live ranges for same variable must be merged Two overlapping live ranges for same variable must be merged a = b a = c a = a + d

22 CS243 Winter 2006 Stanford University 22 Merging Live Ranges Merging definitions into equivalence classes Merging definitions into equivalence classes Start by putting each definition in a different equivalence class Start by putting each definition in a different equivalence class For each point in a program For each point in a program If variable is live and there are multiple reaching definitions for the variable If variable is live and there are multiple reaching definitions for the variable Merge the equivalence classes of all such definitions into one equivalence class Merge the equivalence classes of all such definitions into one equivalence class From now on, refer to merged live ranges simply as live ranges From now on, refer to merged live ranges simply as live ranges

23 CS243 Winter 2006 Stanford University 23 Algorithm for Edges Algorithm Algorithm For each instruction i For each instruction i Let x be live range of definition at instruction i Let x be live range of definition at instruction i For each live range y present at end of instruction i For each live range y present at end of instruction i Insert en edge between x and y Insert en edge between x and y

24 CS243 Winter 2006 Stanford University 24 Example b = d = a (d2) = b + d c = d = a (d1) = d + c a = d (a2) = a liveness {a} {a,c} {c,d} {d} reaching def {a1} {a1,c} {a1,c,d1} {d} {a} {a1,b,c,d1,d2} {a2,b,c,d1,d2} doesn’t use a,b,c or d

25 CS243 Winter 2006 Stanford University 25 Interference Graph t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 t1-t5 are not live

26 CS243 Winter 2006 Stanford University 26 Interference Graph t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 t1-t5 are not live

27 CS243 Winter 2006 Stanford University 27 Interference Graph t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 t1-t5 are not live

28 CS243 Winter 2006 Stanford University 28 Step 2: Coloring Reminder: coloring for n>2 is NP-complete Reminder: coloring for n>2 is NP-complete Observations Observations A node with degree < n? A node with degree < n? A node with degree = n? A node with degree = n? A node with degree > n? A node with degree > n?

29 CS243 Winter 2006 Stanford University 29 Coloring Algorithm Algorithm Algorithm Iterate until stuck or done Iterate until stuck or done Pick any node with degree < n Pick any node with degree < n Remove the node and its edges from the graph Remove the node and its edges from the graph If done (no nodes left) If done (no nodes left) Reverse process and add colors Reverse process and add colors Note: degree of a node may drop in iteration Note: degree of a node may drop in iteration

30 CS243 Winter 2006 Stanford University 30 Colorable by 3 Colors? t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 t1-t5 are not live

31 CS243 Winter 2006 Stanford University 31 Colorable by 3 Colors? Pick t5 and remove its edges t1 t2 t3 t4

32 CS243 Winter 2006 Stanford University 32 Colorable by 3 Colors? Pick t4 and remove its edges t1 t2 t3

33 CS243 Winter 2006 Stanford University 33 Colorable by 3 Colors? Pick t3 and remove its edges t1 t2

34 CS243 Winter 2006 Stanford University 34 Colorable by 3 Colors? Pick t2 and remove its edges t1

35 CS243 Winter 2006 Stanford University 35 Register Assignment t1/r1 Reverse process and add color different from all its neighbors Reverse process and add color different from all its neighbors

36 CS243 Winter 2006 Stanford University 36 Register Assignment t1/r1 t2/r2 Color t2 Color t2

37 CS243 Winter 2006 Stanford University 37 Register Assignment t1/r1 t2/r2 t3/r3 Color t3 Color t3

38 CS243 Winter 2006 Stanford University 38 Register Assignment t1/r1 t2/r2 t3/r3 t4/r3 Color t4 Color t4

39 CS243 Winter 2006 Stanford University 39 Register Assignment t1/r1 t2/r2 t3/r3 t4/r3 t5/r1 Color t5 Color t5

40 CS243 Winter 2006 Stanford University 40 After Register Allocation r1 = 1 r2 = 2 r3 = r1 + r2 r3 = r1 + 3 r1 = r1 + r2 t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2

41 CS243 Winter 2006 Stanford University 41 When Coloring Fails Use heuristics to improve its chance of success and to spill code Use heuristics to improve its chance of success and to spill code Algorithm Algorithm Iterate until done Iterate until done If there exists a node v with degree < n If there exists a node v with degree < n Place v on stack to register allocate Place v on stack to register allocate Else Else Pick a node v to spill using heuristics (e.g. least frequently executed, with many neighbors etc) Pick a node v to spill using heuristics (e.g. least frequently executed, with many neighbors etc) Remove v and its edges from the graph Remove v and its edges from the graph If done (no nodes left) If done (no nodes left) Reverse process and add colors Reverse process and add colors

42 CS243 Winter 2006 Stanford University 42 Colorable by 2 Colors? t1 = 1 t2 = 2 t3 = t1 + t2 t4 = t1 + 3 t5 = t1 + t2 t1 t2 t3 t4 t5 t1-t5 are not live

43 CS243 Winter 2006 Stanford University 43 Colorable by 2 Colors? Pick t5 and remove it edges t1 t2 t3 t4 Need to spill!

44 CS243 Winter 2006 Stanford University 44 Is the Algorithm Optimal? t5 t1 t2 t3 t4 t1 t2 t3 t4 2-colorable? 3-colorable?

45 CS243 Winter 2006 Stanford University 45 Summary Problems: Problems: Given n registers in a machine, is spilling avoidable? Given n registers in a machine, is spilling avoidable? Find an assignment for all pseudo-registers. Find an assignment for all pseudo-registers. Solution Solution Abstraction: an interference graph Abstraction: an interference graph Nodes: merged live ranges Nodes: merged live ranges Edges: presence of live range at time of definition Edges: presence of live range at time of definition Register allocation and assignment problems = n=colorability of interference graph (NP-complete) Register allocation and assignment problems = n=colorability of interference graph (NP-complete) Heuristics to find an assignment for n colors Heuristics to find an assignment for n colors Successful: colorable, and finds assignment Successful: colorable, and finds assignment Not successful: colorability unknown and no assignment Not successful: colorability unknown and no assignment

46 CS243 Winter 2006 Stanford University 46 backup

47 CS243 Winter 2006 Stanford University 47 Predication Example s = s + a *p = s a < b cmp.lt p1,p2=a,b (p1) s = s + a (p2) s = s + b *p = s s = s + b

48 CS243 Winter 2006 Stanford University 48 Predication Identifying region of basic blocks based on resource requirement and profitability (branch misprediction rate, misprediction cost, and parallelism). Identifying region of basic blocks based on resource requirement and profitability (branch misprediction rate, misprediction cost, and parallelism). Result: a single predicated basic block Result: a single predicated basic block

49 CS243 Winter 2006 Stanford University 49 Predicate-aware Register Allocation Use the same register for two separate computations in the presence of predication, even if there is live-range overlap without considering predicates

50 CS243 Winter 2006 Stanford University 50 Example (p1) r32 = 10 (p2) r32 = 20 ;; (p1) st4 [r33]= r32 (p2) r34 = r32 + 1 ;; (p1) v1 = 10 (p2) v2 = 20 ;; (p1) st4 [v10]= v1 (p2) v11 = v2 + 1 ;; v1 v2 overlapped live ranges same register for v1 and v2


Download ppt "Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation."

Similar presentations


Ads by Google