C-4.1 4 Register Allocation Use of registers: 1. intermediate results of expression evaluation 2. reused results of expression evaluation (CSE) 3. contents of frequently used variables 4. parameters of functions, function result (cf. register windowing) 5. stack pointer, frame pointer, heap pointer, ... Number of registers is limited - for each register class: address, integer, floating point Specific allocation methods Register allocation aims at reduction of for different context ranges: * number of memory accesses * 4.1 expression trees (Sethi, Ullman) * spill code, i. e. instructions that store and reload the contents of registers * 4.2 basic blocks (Belady) * 4.3 control flow graphs (graph coloring) Symbolic registers: allocate a new symbolic register to each value assignment (single assignment, no re-writing); defer allocation of real registers to a later phase. (c) 2013 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Compilation Methods SS 2013 / Slide 401 Objectives: Overview on register allocation In the lecture: Explain the use of registers for different purposes. Suggested reading: Kastens / Übersetzerbau, Section 7.5 -------------------------------------------------------------------------------- C-4.2 Register Windowing Register windowing: Berkley Risc: * Fast storage of the processor is accessed r31 ... through a window. r26 22 regs in window 16 shifted * The n elements of the window are used as r25 6 overlapped registers in instructions. ... r16 * On a call the window is shifted by m a, b have to be kept in different registers Life-times for CFGs are determined by data-flow analysis. Graph is "colored" with register numbers. NP complete problem; heuristic technique for coloring with k colors (registers): eliminate nodes of degree < k (and its edges) if the graph is finally empty: graph can be colored with k colors assign colors to nodes in reverse order of elimination else graph can not be colored this way select a node for spilling repeat the algorithm without that node (c) 2011 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Compilation Methods SS 2013 / Slide 408 Objectives: Overlapping life-times modelled by interference graphs In the lecture: * Explain the interference graph using the example of C-4.9. * Demonstrate the heuristics. Suggested reading: Kastens / Übersetzerbau, Section 7.5.4 Questions: * Why is DFA necessary to determine overlapping life-times? Why can't one check each block separately? Give an example where that simplified approach would yield wrong results. * Show a graph that is k-colorable that is not colored successfully by this heuristic. -------------------------------------------------------------------------------- C-4.9 Example for Graph Coloring variables in memory: x, y, z CFG with definitions and uses of variables variables considered for register alloc.: a, b, c, d, e, f a := x f a results of live variable analysis: B1 c := y b, d, e f := z c a, c, f a, c a, c, f f a contribution to a B1 f a interference graph B2 c b := a+1 b := f+a B3 c b c b a, b, c a, b, c a, b, c a, b d := c a d B4 a d d := a e := a+b B5 interference graph c b e b e := b e b, d, e b, d, e b, d, e d2 d1 d3 f a d B6 z := b+d d y := e b e c b e d3 d2 d1 (c) 2013 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Compilation Methods SS 2013 / Slide 409 Objectives: Example for C-4.8 In the lecture: Explain the example Suggested reading: Kastens / Übersetzerbau, Section 7.5.4, Fig. 7.5-6 Assignments: * Apply the technique for another example. Questions: * Why is variable b in block B5 alive? --------------------------------------------------------------------------------