1 / 45

Register Allocation

Register Allocation. Interference graph Register allocation via graph colorability. Register Allocation: Why?. Unbounded number of temporaries and variables and bounded (very less) number of registers Temporary variables need to be replaced with some fixed set of registers

Download Presentation

Register Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Register Allocation Interference graph Register allocation via graph colorability

  2. Register Allocation: Why? • Unbounded number of temporaries and variables and bounded (very less) number of registers • Temporary variables need to be replaced with some fixed set of registers • Liveness analysis finds the variables which are live after each instruction • Two simultaneously live variables cannot be allocated to the same register

  3. Interference graphs • A condition that prevents two variables to be assigned to the same register is said to be ‘interference’ • The temporaries which are live at a time are said to be interfering and cannot be assigned to the same register • The overlapping live ranges cause interference • If an instruction cannot access a register r1 to generate a variable a, r1 interferes with a

  4. Interference Graph • The interference graph is used for assigning registers to temporary variables. • If two variables do not interfere then we can use the same register for both of them, thus reducing the number of registers needed. • if there is a graph edge between two variables, then same register cannot be assigned

  5. Register allocation • For every node n in Control Flow Graph, we have out[n] • Set of temporaries live out of n • Two variables interfereif • both initially live (ie: function args), or • both appear in out[n] for any n, or • one is defined and the other is in out[n] • x = b - c where x is dead & b is live interfere • How to assign registers to variables?

  6. Interference graph • Nodes of the graph = variables • Edges connect variables that interfere with one another • Nodes are assigned a color corresponding to the register assigned to the variable • Two colors can’t be next to one another in the graph

  7. Graph coloring problem Can this graph be colored with 2 colors in such a way that no two neighbors get the same color?

  8. Graph coloring problem Cannot be colored with the same color, therefore use the third color Can this graph be colored with 2 colors in such a way that no two neighbors get the same color? Possible only with three colors

  9. Example 1 : Planar graph can be colored in maximum 4 colors Each region is modeled as a vertex Each boundary between two adjacent regions forms an edge

  10. Example2 : Schedule the exam slots using graph coloring Suppose that in a particular quarter there are students taking each of the following combinations of courses: • Math, English, Biology, Chemistry • Math, English, Computer Science, Geography • Biology, Computer Science, History, French • English, Psychology, Computer Science, History • Psychology, Chemistry, Computer Science, French • Psychology, Geography, biology, Spanish What is the minimum number of examination periods required for the exams in the ten courses specified so that students taking any of the given combinations of courses have no conflicts? Find a schedule that uses this minimum number of periods.

  11. Exam scheduling via graph coloring 5 7 Math Eng 7 6 9 Bio Geo Hist 8 6 CS che 3 Spa Fre 5 8 Psy

  12. Math Eng Bio Geo Hist CS che Spa Fre Psy

  13. Heuristic: select a node with 5 or less edgesstep 1: push ‘Math’ on the stack 5 6 7 Math Eng 6 7 6 8 9 Bio Geo Hist 7 8 5 6 CS che 3 Spa Fre 5 8 Psy

  14. Heuristic: select a node with 5 or less edgesstep 2: push ‘Chemistry’ on the stack 6 5 7 Eng 6 7 6 8 7 9 Bio Geo Hist 8 6 7 5 CS che 3 Spa Fre 5 4 7 8 Psy

  15. Heuristic: select a node with 5 or less edgesstep 3: push ‘English’ on the stack 5 Eng 5 6 7 5 6 6 9 7 8 Bio Geo Hist 6 7 8 5 CS 3 Spa Fre 5 4 7 8 6 Psy

  16. Heuristic: select a node with 5 or less edgesstep 4: push ‘CS’ on the stack 4 5 6 7 4 5 6 5 6 7 8 9 Bio Geo Hist 8 7 6 5 CS 3 Spa Fre 5 3 4 7 8 6 5 Psy

  17. Heuristic: select a node with 5 or less edgesstep 5: push ‘Bio’ on the stack 4 5 6 7 4 5 6 3 5 6 8 7 9 Bio 3 Geo Hist 3 2 Spa Fre 5 4 2 3 7 4 8 6 5 Psy

  18. Heuristic: select a node with 5 or less edgesstep 6: push ‘Psy’ on the stack 2 4 5 6 7 2 3 4 5 6 3 Geo Hist 2 3 1 Spa Fre 1 5 4 3 2 Psy 4

  19. Heuristic: select a node with 5 or less edgesstep 7: push ‘Geo’ on the stack 2 2 3 5 4 6 1 Geo Hist 0 1 3 2 Spa Fre 1 2 5 4 3 1

  20. Hist Heuristic: select a node with 5 or less edgesstep 8: push ‘Hist’ on the stack 1 Hist 0 1 2 3 Spa Fre 1 3 2 5 1 4 0

  21. Color the remaining nodes as ‘green’ • Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Hist Math Eng Bio Geo Hist CS che Spa Spa Fre Fre Psy

  22. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Geo Psy Bio CS Eng Math Che Eng Math Bio Geo Hist Hist CS che Spa Spa Fre Fre Psy

  23. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Psy Bio CS Eng Math Che Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  24. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Bio CS Eng Math Che Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  25. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors CS Eng Math Che Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  26. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Eng Math Che Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  27. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Math Che Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  28. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Math Eng Math Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  29. Mark all the out edges as red • Keep popping off the subjects, color the nodes with a color different from its neighbors Math Eng Bio Geo Geo Hist Hist CS che Spa Spa Fre Fre Psy

  30. Math, English, Biology, Chemistry • Math, English, Computer Science, Geography • Biology, Computer Science, History, French • English, Psychology, Computer Science, History • Psychology, Chemistry, Computer Science, French • Psychology, geography, Biology, Spanish Scheduling

  31. Conclusion • Number of courses = 10 • Number of combinations = 6 • Number of courses in each combination = 4 • Number of Clash free Exam slots = 6

  32. Register allocation as graph coloring problem • Register allocation can then be reduced to the problem of K-coloring the resulting graph, where K is the number of registers available on the target architecture. • No two vertices sharing an edge may be assigned the same color, and vertices sharing a preference edge should be assigned the same color if possible.

  33. Consider the following Example a  0 b  a + 1 c  c + b a  b * 2 a < 100 return c Sundar B., BITS, Pilani. CS C362

  34. LivenessAnalysis –Computation** Sundar B., BITS, Pilani. CS C362 **Discussed in the previous class

  35. Consider the following Example Live-in {c} Live-out {a,c} a  0 Live-in {a,c} Live-out {b,c} b  a + 1 Live-in {b,c} Live-out {b,c} c  c + b Live-in {b,c} Live-out {a,c} a  b * 2 Live-out {a,c} Live-in {a,c} a < 100 Live-in {c} return c

  36. Build the interference graph • Variables that are live at the same time c ac bc bc ac a at various program points

  37. Class Assignment 1 • Q1. Write the target code for the given IR code • With 1 register (if possible) • With 2 registers a=0 b=a+1 c=c+b a=b*b if(a<100) return c Q2. What is the total number of registers used?

  38. Interference graph a a a Program state start b b Node 2 entry Node 1 entry Node 1 exit Node 2 exit Node 3 entry c c And so on… Variables that need registers to be allocated to them

  39. Register allocation • Some of the vertices may be pre-colored to begin with(parameters for example). • As graph coloring in general is NP-complete, so is register allocation.

  40. Graph coloring • Questions: • Can we efficiently find a coloring of the graph whenever possible? • Can we efficiently find the optimum coloring of the graph? • How do we choose registers to avoid move instructions? • What do we do when there aren’t enough colors (registers) to color the graph? Resource: google search

  41. Register allocation algorithm • Basic steps • Build the interference graph • Simplify and color the graph (using a stack) • Spill the node (put back in the memory) • select

  42. Spill • An interference graph is created and an edge between two variable nodes is added if they are simultaneously live • If number of available registers (other than special purpose) is K, and if any node is of degree >=K, then the heuristic fails and • Some nodes (variables) are marked for spilling (to memory)

  43. sum=0 i=0 t1= a[i] Control Flow Graph B1 Class Assignment 2: Q1. compute liveness of the variablesQ2. Construct the interference graph for the given set of variables B2 if(i<10) goto B4 goto B5 B3 sum=sum+t1 i=i+1 t1=a[i] goto B2 B4 B5 result=sum

  44. Coalescing • Used in optimizing the code by eliminating the redundant move instructions • Consider the source and destinations of the MOVE instruction • If there is no edge between these two in the interference graph, then MOVE can be eliminated

  45. Class Assignment: • Design such redundant MOVE instructions first • Draw the interference graph • Eliminate the redundant MOVE instructions • The source and destination nodes are coalesced into a new node whose edges are the union of the edges of the replaced nodes

More Related