



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Liveness information allows us to keep values in registers if they will be used later. An obvious concern is why do we assume all variables are live at the end of blocks? Why do we need to save live variables at the end? It seems reasonable to perceive that we might have to reload them in the next block. To do this, we need to determine live/next use information across blocks and not just within a block. This requires global data-flow analysis.
Global Data-Flow Analysis
A Directed Acyclic Graph (DAG) for a basic block has the following labels for the nodes:
Algorithm: Generate DAG from 3-address code
For statement i: x = y op z
Here and an example of the DAG generated for the 3-address code
a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t
b’
t
c’
t
b +
c
a
Here is another example
t1 = 4 * i t2 = a[t1] t3 = 4 * i t4 = b[t3] t5 = t2 * t t6 = p + t p = t t7 = i + 1 i = t if i <20 goto (1)
a b 4 i’ 1
[ ] (^) [ ]^ <
p’ (^) t
t2 t
t1,t t7,i
(1)
t6,p
20
Whereas, order #2 with 2 registers leads to
MOV c, R ADD D, R MOV e, R SUB R0,R MOV a,R ADD b, R MOV R0,t
Only seven instructions are required, a saving of three machine instructions. Reordering improved code because computation of t4 immediately followed computation of t1, its left operand. t1 must be in a register and it is.
Register Allocation
Registers in a machine are a scarce resource. The issue faced by the code generator is this how to best use the bounded number of registers. The matter is complicated by the fact that a few registers are reserved for special purposes. For example, the program counter is kept in a registers. A register is used for the keeping track of the top of the function call stack. Certain operators require multiple registers, often in pairs. Division is an example of such an operator.
The general register allocation problem is NP-complete. Heuristic algorithms exist to solve the problem. One such strategy is the by using the graph coloring algorithm: given a graph, color the nodes with different colors such that no two nodes that have an edge between them have the same color. Here is how the graph coloring algorithm can be used to compute register allocation for K registers in a machine.
Algorithm: K registers allocation with graph coloring
Process may require iterations and rewriting of some of the code to create more temporaries.
Let us apply the algorithm to the following 3-address code
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}
The interference graph generated is
a
b
t
c
t
Upon coloring the nodes, the final register allocation assuming 3 registers is
a c
b
t
t R3 R