Download Basic Code Generation, DAG Representations - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity! Compiler Construction Lecture 45 Docsity.com 2 Liveness information allows us to keep values in registers if they will be used later (efficiency) Basic Code Generation Docsity.com 5 DAG Representations A dag (directed acyclic graph) for a basic block has the following labels for the nodes: Docsity.com 6 DAG Representations Leaves are labeled by unique identifiers. Interior nodes are labeled by operator symbols. Nodes can have multiple labels since they represent computed values. Docsity.com 7 DAG Representations BB DAG for x = y op z op x y z Docsity.com 10 Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 Docsity.com 11 Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 b c + a Docsity.com 12 Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 b t1 c + * a Docsity.com 15 Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 b’ t1 c’ t2 + * + b * + c a Docsity.com 16 Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 b’ t1 c’ t2 + * + b * + + c a Docsity.com 17 Example 2 t1 = 4 * i t2 = a[t1] t3 = 4 * i t4 = b[t3] t5 = t2 * t4 t6 = p + t5 p = t6 t7 = i + 1 i = t7 if i <20 goto (1) a b 4 i’ 1 + * < [ ] [ ] * + p’ t6,p t5 t2 t4 t1,t3 t7,i (1) 20 Docsity.com 20 Order #1 with 2 registers t1 = a + b t2 = c + d t3 = e – t2 t4 = t1 – t3 MOV a, R0 ADD b,R0 MOV c, R1 ADD D, R1 MOV R0,t1 MOV e, R0 SUB R1,R0 MOV t1,R1 SUB R0, R1 MOV R1,t4 ; 10 instructions Docsity.com 21 Order #2 with 2 registers t2 = c + d t3 = e – t2 t1 = a + b t4 = t1 – t3 MOV c, R0 ADD D, R0 MOV e, R1 SUB R0,R1 MOV a,R0 ADD b, R0 MOV R0,t4 ; 7 instructions Docsity.com 22 Order 1 t1 = a + b t2 = c + d t3 = e – t2 t4 = t1 – t3 Order 2 t2 = c + d t3 = e – t2 t1 = a + b t4 = t1 – t3 Reordering improved code because computation of t4 immediately followed computation of t1, its left operand. t1 must be in a register and it is Docsity.com 25 Register Allocation Complications: • special purpose registers • operators requiring multiple registers. Docsity.com 26 Algorithm: K registers 1. Compute liveness information. 2. Create interference graph G • one node for each variable, • an edge connects two variables if one is live at a point where the other is defined Docsity.com 27 Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} inteference graph Docsity.com 30 Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a b t1 c inteference graph Docsity.com 31 Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a b t1 c t2 inteference graph Docsity.com 32 Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a b t1 c t2 inteference graph Docsity.com 35 5. Assign colors - Starting with empty graph, rebuild graph by popping elements off the stack and assigning a color different from neighbors. Docsity.com 36 5. (continued) Potential spill nodes may or may not be colorable. Process may require iterations and rewriting of some of the code to create more temporaries. Docsity.com 37 Example, k = 3 a b t1 c t2 Assume k = 3 Remove t1 t1 Docsity.com 40 Example c t2 Assume k = 3 Remove c t1 a b c Docsity.com 41 Example Assume k = 3 Remove t2 t1 a b c t2 Docsity.com 42 Rebuild Graph Assume k = 3 t1 a b c t2 Docsity.com 45 Example a b t1 c t2 Assume k = 3 R1 R2 R3 t1 a Docsity.com 46 Example a b t1 c t2 Assume k = 3 R1 R2 R3 R1 t1 Docsity.com 47 Example a b t1 c t2 Assume k = 3 R1 R2 R3 R1 R2 chromatic number is 3 Docsity.com