Using liveness for register allocation
Code Generation & Register Allocation

Answer & solution
Official answer: B
Approach: Ask which optimization needs to know whether an existing variable value can be used again.
A variable is live at a program point if some execution path may read its current value before assigning a new value to that variable. Registers hold only a limited number of such values. If two values must remain available at the same time, allocating the same physical register to both can destroy a value that will be needed.
Register allocation therefore uses live ranges, commonly creating an interference edge between values that are simultaneously live. Values whose live ranges do not overlap can reuse a register. This selects B.
Strength reduction, C, replaces an expensive operation with an equivalent cheaper one, while constant folding, D, evaluates expressions with known constants. Neither is identified by the need for liveness in this question. Runtime call management concerns the calling convention and activation records rather than this particular analysis.







![Question 54, GATE 2007: A computer has only two registers. Instructions OP Rj,Ri store Rj OP Ri in Ri; OP m,Ri store M[m] OP Ri in Ri; OP is ADD or SUB. MOV m,Ri loads memory into a register and MOV Ri,m stores a register to memory. Generate code for t1=a+b; t2=c+d; t3=e−t2; t4=t1−t3. All operands start in memory and the final result must be in memory. What minimum MOV count is intended?
Options:
(A) 2
(B) 3
(C) 5
(D) 6](../../../assets/questions/CS-2007-Q54-1.webp)


![Question 23, GATE 1994: Generate code for A→id:=E; E→E+T | T; T→(E) | id. The target has one accumulator R and only LOAD x (R:=mem[x]), STORE x (mem[x]:=R), ADD x (R:=R+mem[x]). Provide a syntax-directed translation.](../../../assets/questions/CS-1994-Q77-1.webp)


