Chapter 9: Code generation

Rearranging order .

Three adress code for the DAG (assuming only two registers are available)  

Rearranging the code as

t2 = c + d

t3 = e -t 2

t1 = a + b

MOV a, R0   X = t 1 -t3
ADD b, R0   gives
MOV c, R 1   MOV c, R 0
ADD d, R 1   ADD d, R 0
MOV R0 , t1 Register spilling MOV e, R 1
MOV e, R0   SUB R 0 , R1
SUB R 1 , R0   MOV a, R 0
MOV t1 , R 1 Register reloading ADD b, R0
SUB R 0 , R1   SUB R 1 , R0
MOV R1 , X   MOV R 1 , X
   

If we generate code for the three-address statements using the code generation algorithm described before, we get the code sequence as shown (assuming two registers R0 and R1 are available, and only X is live on exit). On the other hand suppose we rearranged the order of the statements so that the computation of t 1 occurs immediately before that of X as:

t2 = c + d

t3 = e -t 2

t1 = a + b

X = t 1 -t3

Then, using the code generation algorithm, we get the new code sequence as shown (again only R0 and R1 are available). By performing the computation in this order, we have been able to save two instructions; MOV R0, t 1 (which stores the value of R0 in memory location t 1 ) and MOV t 1 , R1 (which reloads the value of t 1 in the register R1).