register allocation ii
play

Register Allocation II 15-745 Optimizing Compilers Spring 2006 - PowerPoint PPT Presentation

Register Allocation II 15-745 Optimizing Compilers Spring 2006 David Koes School of Computer Science Outline Review Old Subtleties New Subtleties School of Computer Science Review Build Simplify Coalesce Potential Spill


  1. Register Allocation II 15-745 Optimizing Compilers Spring 2006 David Koes School of Computer Science

  2. Outline • Review • Old Subtleties • New Subtleties School of Computer Science

  3. Review Build Simplify Coalesce Potential Spill Select Actual Spill School of Computer Science

  4. Build First compute live ranges v <- 1 - use both reaching definitions w <- v + 3 and liveness information x <- w + v - live range defined by v definition point u <- v x - ends when variable dies t <- u + x w <- w t <- t u <- u School of Computer Science

  5. Build Construct interference graph: - each node represents a live range v - edges represent live ranges that overlap - put in move edges between move operands m o v x w e e d v <- 1 g e w <- v + 3 v x <- w + v x u u <- v w t <- u + x t <- w u <- t t <- u School of Computer Science

  6. Simplify k = 4 Reduce the graph: - remove non-move related, easy to color, nodes v - easy to color: degree < k - place on stack x w w u x t t School of Computer Science

  7. Coalesce Coalesce moves: k = 4 - conservatively combine operands of a move v - subtlety: how? Repeat Simplify uv uv -Detail: If both Simplify and Coalesce get stuck, start simplifying move related nodes w u Simplify x Coalesce t School of Computer Science

  8. What if we can’t simplify? Now what? k = 3 Be optimistic: v - Put a node with degree ≥ k on stack - Lose guarantee that anything we put on stack is colorable x w - If we’re lucky this node will still be colorable when popped from stack Be realistic: t u - If unlucky, this node will have to be spilled (allocated to memory) w - Mark as potential spill to avoid t recomputation later v School of Computer Science

  9. Select Pop a node from the stack k = 3 Assign it a color that does not v conflict with neighbors in interference graph x This will always be possible, x w u unless the node is a potential spill t u If it is not possible spill variable and rebuild graph w t v School of Computer Science

  10. Spilling v <- 1 Allocate w to memory w 1 <- v + 3 location M w w 1 M w []<- w 1 Spilled variables are allocated to w 2 <- M w [] w 2 the stack in an area completely controlled by the compiler. x <- w 2 + v These memory locations are v special in that they can be u <- v optimized without concern for x t <- u + x memory aliasing issues. w 3 <- M w [] w 3 <- w 3 t <- t Now Start Over... u <- u ...compute live ranges... School of Computer Science

  11. Spilling to Memory • RISC Architectures – Only load and store can access memory • every use requires load • every def requires store • create new temporary for each location • CISC Architectures – can operate on data in memory directly • makes writing compiler easier(?), but isn’t necessarily faster – pseudo-registers inside memory operands still have to be handled School of Computer Science

  12. Rematerialization An alternative to spilling – Recompute value of variable instead of store/load to memory – Example: v <- 1 v <- 1 w <- v + 3 w <- v + 3 x <- w + v x <- w + v u <- v u <- v t <- u + x t <- u + x <- w w <- 4 <- t <- w <- u <- t <- u School of Computer Science

  13. Build Take Two v <- 1 k = 3 w 1 <- v + 3 v M w []<- w 1 w 2 <- M w [] x <- w 2 + v x w 1 w 2 w 3 u <- v t <- u + x u w 3 <- M w [] <- w 3 <- t t <- u Recalculate interference graph School of Computer Science

  14. Simplify->Coalesce->Select k = 3 v x w 1 w 2 w 3 uv u t School of Computer Science

  15. Review Build Simplify Coalesce Potential Spill Select Actual Spill School of Computer Science

  16. Old Subtleties 1. MOVE instructions 2. Merging live ranges 3. Splitting live ranges 4. Choosing potential spills 5. Allocating spill slots 6. Coalescing is bad School of Computer Science

  17. #1 MOVE Instructions • During liveness analysis, MOVE instructions should be treated specially t := s … Note that s and t don’t x <- ⊗ (…s…) really interfere … y <- ⊗ (…t…) Under what conditions can we remove the interference edge between s and t ? School of Computer Science

  18. #2 Live Ranges & Merged Live Ranges 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. – How to compute a live range? a = … a = … … = a Two overlapping live ranges for the same variable must be merged School of Computer Science

  19. Example Live Variables {} {} Reaching Definitions A = ... (A 1 ) {A} {A 1 } IF A goto L1 {A} {A 1 } {A} {A 1 } B = ... (B 1 ) L1: {A} {A 1 } {A,B} {A 1 ,B 1 } = A C = ... (C 1 ) {A,C} {A 1 ,C 1 } {B} {A 1 ,B 1 } D = B (D 2 ) = A {C} {A 1 ,C 1 } {D} {A 1 ,B 1 ,D 2 } D = ... (D 1 ) {D} {A 1 ,C 1 ,D 1 } {D} {A 1 ,B 1 ,C 1 ,D 1 ,D 2 } A = 2 (A 2 ) Merge {A,D} {A 2 ,B 1 ,C 1 ,D 1 ,D 2 } {A,D} {A 2 ,B 1 ,C 1 ,D 1 ,D 2 } = A {D} {A 2 ,B 1 ,C 1 ,D 1 ,D 2 } ret D A live range consists of a definition and all the points in a program in which that definition is live. School of Computer Science

  20. Merging Live Ranges • Merging definitions into equivalence classes: – Start by putting each definition in a different equivalence class – For each point in a program • if variable is live, and there are multiple reaching definitions for the variable • merge the equivalence classes of all such definitions into a one equivalence class Merged live ranges are also known as “webs” School of Computer Science

  21. Example: Merged Live Ranges {} A = ... (A 1 ) {A 1 } IF A goto L1 {A 1 } {A 1 } B = ... (B 1 ) L1: {A 1 } {A 1 ,B 1 } = A C = ... (C 1 ) {A 1 ,C 1 } {B 1 } D = B (D 2 ) = A {C 1 } {D 1,2 } D = ... (D 1 ) {D 1,2 } {D 1,2 } A = 2 (A 2 ) {A 2 ,D 1,2 } {A 2 ,D 1,2 } = A {D 1,2 } ret D A has two “webs” makes register allocation easier School of Computer Science

  22. Edges of Interference Graph Intuitively: Two live ranges (necessarily of different variables) may interfere if they overlap at some point in the program. Algorithm: At each point in program, enter an edge for every pair of live ranges at that point An optimized definition & algorithm for edges: For each defining inst i Let x be live range of definition at inst i For each live range y present at end of inst i insert an edge between x and y Faster? {D 1,2 } Edge between A 2 and D 1,2 A = 2 (A 2 ) Better quality? {A 2 ,D 1,2 } School of Computer Science

  23. Reducing Register Pressure Splitting variables into live ranges creates an interference graph that is easier to color Eliminate interference in a variable’s “dead” zones. Increase flexibility in allocation: can allocate same variable to different registers A = … B = … C = … … = A … = A D = B D = C A = D ret A School of Computer Science

  24. #3 Live Range Splitting Split a live range into smaller regions (by paying a small cost) to create an interference graph that is easier to color Eliminate interference in a variable’s nearly dead zones. Cost: Memory loads and stores Load and store at boundaries of regions with no activity # active live ranges at a program point can be > # registers Can allocate same variable to different registers Cost: Register operations a register copy between regions of different assignments # active live ranges cannot be > # registers School of Computer Science

  25. Examples Example 1: Example 2: A = …; B = …; A = … FOR i = 0 TO 10 FOR j = 0 TO 10000 A = A + ... (does not use B) B = … C = … FOR j = 0 TO 10000 … = A+B … = A+C B = B + ... C = … B = … (does not use A) … = B+C School of Computer Science

  26. One Algorithm • Observation: Spilling is absolutely necessary if not degree in graph – number of live ranges active at a program point > n • Apply live-range splitting before coloring – Identify a point where number of live ranges > n – For each live range active around that point • find the outermost “block construct” that does not access the variable – Choose a live range with the largest inactive region – Split the inactive region from the live range School of Computer Science

  27. #4 What to Spill? When choosing potential spill node want: – A node that makes graph easier to color • Fewer spills later – A node that isn’t “expensive” to spill • An expensive node would slow down the program if spilled – We can apply heuristics both when choosing potential spill nodes and when choosing actual spill nodes • not required to spill node that we popped off stack and can’t color School of Computer Science

  28. A Spill Heuristic Pick node (live range) n that minimizes: � � 10 depth ( def ) 10 depth ( use ) + def � n use � n degree( n ) This heuristic prefers nodes that: – Are used infrequently – Aren’t used inside of loops – Have a large degree Could use any one of several other heuristics as well… School of Computer Science

  29. #5 Spill coalescing • On machines with few registers (like the Pentium), a lot of temps can get spilled – activation records get big – memory-to-memory moves get generated • For these reasons, it is good to do spill coalescing School of Computer Science

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend