Register Allocation Back End Structure After Instruction Selection

Register Allocation Back End Structure After Instruction Selection

Register Allocation 15-745 Optimizing Compilers Spring 2008 School of Computer Science School of Computer Science Back end structure After Instruction Selection define i32 @erk(i32 %a, i32 %b) {! entry:! ! %tmp3 = mul i32 %a, 37!! ; <i32> [#uses=1]! ! %tmp4 = mul i32 %tmp3, %a!! ; <i32> [#uses=1]! instruction ! %tmp6 = add i32 %b, 33!! ; <i32> [#uses=1]! Assem ! %tmp7 = sdiv i32 %tmp4, %tmp6!! ; <i32> [#uses=1]! IR selector ! %tmp9.neg = xor i32 %a, -1!! ; <i32> [#uses=1]! ! %tmp10 = add i32 %tmp7, %tmp9.neg!! ; <i32> [#uses=1]! ! ret i32 %tmp10! }! register entry: 0x803ce0, LLVM BB @0x801a90, ID#0:! allocator TempMap ! %reg1024 = MOV32ri 33! ! %reg1025 = ADD32rm %reg1024, <fi#-2>, 1, %NOREG, 0! ! %reg1026 = MOV32rm <fi#-1>, 1, %NOREG, 0! ! %reg1027 = IMUL32rr %reg1026, %reg1026! ! %reg1028 = IMUL32rri8 %reg1027, 37! ! %EAX = MOV32rr %reg1028! instruction ! %EAX = CDQ %EDX<imp-def>, %EAX<imp-use>! Assem ! IDIV32r %reg1025, %EAX<imp-def>, %EDX<imp-def>, %EAX<imp-use>, %EDX<imp-use>! scheduler ! %reg1029 = MOV32rr %EAX! ! %reg1030 = NOT32r %reg1026! ! %reg1031 = ADD32rr %reg1029, %reg1030! ! %EAX = MOV32rr %reg1031! ! RET! School of Computer Science School of Computer Science 1 Abstract View Register allocator’s job …! … …Written Read t7 <- 2! movl $2, t7 t7 <- () The register allocator’s t11 <- t7! movl t7, t11 t11 <- (t7) t11 <- (t7, t11)! job is to assign each t10 <- t11! imull t7, t11 t11 <- (t7, t11) temp to a machine t10 <- t10! movl t11, t10 t10 <- (t11) t8 <- t10! imull $37, t10 t10 <- (t10) register. t17 <- t7! movl t10, t8 t8 <- (t10) t17 <- t17! movl t7, t17 t17 <- (t7) %eax <- ! addl $1, t17 t17 <- (t17) %edx <- (%eax,%edx)! movl $33, %eax %eax <- () If that fails, the register %eax,%edx <- t17! cltd %edx,%eax <- (%eax) allocator must rewrite …! idivl t17 %eax,%edx <- (t17,%eax,%edx) the code so that it can … … … t7 : %ebx succeed. t8 : %ecx t10 : %eax The t11 : %eax Abstract view, for register allocation purposes t17 : %esi TempMap … School of Computer Science School of Computer Science Some terminology A graph-coloring problem Two temps interfere if at some point in the program they cannot both occupy the same Interference graph: an undirected graph where •!nodes = temps register. •!there is an edge between two nodes if their v corresponding temps interfere v <- 1 x w Which temps interfere? v <- 1 w <- v + 3 w <- v + 3 x <- w + v x <- w + v u <- v u <- v t <- u + x u t <- u + x <- w <- w <- t <- t <- u <- u t School of Computer Science School of Computer Science 2 A graph-coloring problem History For early architectures, register allocation was not very A graph is k-colorable if every node in the graph can important be colored with one of k colors such that two adjacent Early work by Cocke (in 1971) proposed the idea that nodes do not have the same color v register allocation can be viewed as a graph coloring problem Assigning k registers = Coloring with k colors Chaitin was the first to implement this idea for the IBM 370 PL/1 compiler, in 1981 x w v <- 1 In 1982, at IBM, Chaitin’s allocator was used for the PL.8 w <- v + 3 compiler for the IBM 801 RISC system x <- w + v eax u <- v Today, register allocation is the most essential of code edx u optimizations t <- u + x ecx <- w <- t <- u t School of Computer Science School of Computer Science History, cont’d Steps in register allocation Motivated by the first MIPS architecture, Chow and Hennessy developed priority-based graph coloring in 1984 “top down” coloring Build Another popular algorithm for register Color allocation based on graph coloring is due to Briggs in 1992 Spill “bottom up” coloring School of Computer Science School of Computer Science 3 Building the interference graph Edges of Interference Graph Given liveness information, we can build the Intuitively: interference graph (IG) Two variables interfere if they overlap at some point in the program. Algorithm: At each point in program, enter an edge for every pair of variables at that point How? v v <- 1 {} w <- v + 3 {v} An optimized definition & algorithm for edges: x <- w + v {w,v} x w u <- v {w,x,v} "" For each defining inst i Let x be definition at inst i t <- u + x {w,u,x} For each variable y live at end of inst i <- x {x,w,u,t} u insert an edge between x and y <- w {w,u,t} Faster? <- t {u,t} {D} A = 2 (A ) Edge between A and D <- u {u} Better quality? 2 {A,D} t School of Computer Science School of Computer Science Building the interference graph A Better Interference Graph for each defining inst i! let x be temp defined at inst i" x = 0;! What does the interference for all y in LIVE-IN of succ(i)! for(i = 0; i < 10; i++)! graph look like? insert an edge between x and y! {! x += i;! v What’s the minimum }! number of registers v <- 1 {} needed? w <- v + 3 {v} x w y = global;! x <- w + v {w,v} y *= x;! u <- v {w,x,v} t <- u + x {w,u,x} <- x {x,w,u,t} u for(i = 0; i < 10; i++)! <- w {w,u,t} {! <- t {u,t} y += i;! <- u {u} t }! School of Computer Science School of Computer Science 4 Live Ranges & Merged Live Ranges Example Live Variables A live range consists of a definition and all Reaching Definitions {} {} A = ... (A1) {A} {A } IF A goto L1 1 the points in a program (e.g. end of an {A} {A1} instruction) in which that definition is live. {A} {A1} B = ... (B ) 1 L1: {A} {A } {A,B} {A1,B1} = A 1 C = ... (C1) –!How to compute a live range? {B} {A1,B1} {A,C} {A1,C1} D = B (D2) = A {D} {A1,B1,D2} {C} {A1,C1} D = ... (D1) a = … a = … {D} {A1,C1,D1} {D} {A ,B ,C ,D ,D } A = 2 (A ) 1 1 1 1 2 Merge … = a 2 {A,D} {A ,B ,C ,D ,D } 2 1 1 1 2 Two overlapping live ranges for the same {A,D} {A2,B1,C1,D1,D2} {D} {A ,B ,C ,D ,D } = A variable must be merged 2 1 1 1 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 School of Computer Science Merging Live Ranges Example: Merged Live Ranges Merging definitions into equivalence classes: {} A = ... (A1) {A } –! Start by putting each definition in a different equivalence class IF A goto L1 1 {A1} –! For each point in a program •! if variable is live, {A1} B = ... (B ) 1 L1: {A } and there are multiple reaching definitions for the variable {A1,B1} = A 1 C = ... (C1) {B1} {A1,C1} •! merge the equivalence classes of all such definitions into a one D = B (D2) = A {D1,2} {C1} equivalence class D = ... (D1) {D1,2} {D1,2} A = 2 (A2) {A2,D1,2} {A ,D } 2 1,2 = A {D1,2} Merged live ranges are also known as “webs” A has two “webs” ret D makes register allocation easier School of Computer Science School of Computer Science 5 Steps in register allocation Steps in register allocation Build Build Prioritize Color Color Select Spill Spill School of Computer Science School of Computer Science Priority Coloring Priority Coloring Heuristic priority function computes Allocate in priority order k = 3 k = 3 priority for each variable Another heuristic selects which –!highest priority allocated first v register to assign to variable v – rotating registers Ideas for priority functions? ! –!lowest numbered register x w x w v <- 1 Priorities: w <- v + 3 v: 4 w: 3 x <- w + v eax x: 2 u u u <- v edx u: 3 t <- u + x ecx <- w t: 2 <- t Order: Order: t t <- u v, w, u, x, t v, w, u, x, t School of Computer Science School of Computer Science 6 Steps in register allocation Graph coloring Once we have the interference graph, we can attempt Simplify register allocation by searching for a K-coloring Build This is an NP-complete problem (K!3)* Coalesce But a linear-time simplification algorithm by Kempe (in 1879) Color tends to work well in practice Potential Spill Spill Select * [1] H. Bodlaender, J. Gustedt, and J. A. Telle, “Linear-time register allocation for a fixed number of registers,” in Proceedings of the ninth annual ACM-SIAM symposium on Discrete algorithms, pp. 574–583, Society for Industrial and Applied Mathematics, 1998. [2] S. Kannan and T. Proebsting, “Register allocation in structured programs,” in Proceedings of the sixth annual ACM-SIAM symposium on Discrete algorithms, pp. 360–368, Society for Industrial and Applied Mathematics, 1995. [3] M. Thorup, “All structured programs have small tree width and good register allocation,” Inf. Comput., vol. 142, no. 2, pp. 159–181, 1998. School of Computer Science School of Computer Science Kempe’s algorithm Kempe’s algorithm, cont’d Basic observation: If all nodes are removed by step #1, then –!given a graph that contains a node with the graph is K-colorable degree less than K, the graph is K-colorable However, the degree<K rule does not iff the graph without that node is K-colorable always work, for example: –!this is called the “degree<K” rule So, step #1 of Kempe’s algorithm: –!iteratively remove nodes with degree<K t2 t3 This graph is 3-colorable, t5 but the degree<3 rule doesn’t work t1 t4 School of Computer Science School of Computer Science 7 Kempe’s algorithm, cont’d Example In step #1, each removed node should be pushed onto a k = 3 stack v –!when all are removed, we pop each node and put it back into v the graph, assigning a suitable color as we go In case we get stuck (i.e., there are no nodes with x degree<K), we apply step #2: x w –!choose a node with degree!K and optimistically remove it, and w then continue u u t t Stack School of Computer Science School of Computer Science Example Another Example Now what? k = 3 k = 3 Be optimistic: v - Put a node with degree ! k on stack v v - Lose guarantee that anything we put on stack is colorable x x w -! If we’re lucky this node will still be x w colorable when popped from stack w Be realistic: t u u

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us