GATE / CS ARCHIVE

SUBJECT COLLECTION

Compiler Design · Code Generation & Register Allocation

11 questions, newest first

GATE CSE has asked 11 questions on Code Generation & Register Allocation (Compiler Design) from 1991 to 2025, in 10 of the 46 papers; the most recent is in GATE CSE 2025 Set 1. In the last five years it carried 0.1 marks per 100-mark paper on average, against 0.4 since 2003. Since 2014 the questions have been 1 MCQ and 1 NAT. 2 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Not in the GATE 2027 syllabus. Code generation and register allocation are not named; the section names intermediate code generation, local optimisation and data-flow analysis.

Browse topics
1 mark · MCQ
Report issue

Using liveness for register allocation

Code Generation & Register Allocation

Question 13, GATE 2025: Which compiler activity uses live-variable analysis? A runtime function-call management; B assigning registers to variables; C strength reduction; D constant folding.
Tap to enlarge
Your answer
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.

Original paper, page 13 · Answer key

2 marks · NAT
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 52, GATE 2017: For (a−1)*(((b+c)/3)+d), find minimum registers for optimal load/store code generation without spills. Only loads/stores can access memory; arithmetic operands must be registers or immediate constants.
Tap to enlarge
Answer & solution

Official answer: 2.0

Our answer: 2

1. Evaluate the more demanding right subtree first. Load b into R1 and c into R2; add into R1. Divide R1 by immediate 3, then reuse R2 to load d and add it to R1. Now R1 holds ((b+c)/3)+d.

2. Keep R1 live and load a into R2. Subtract immediate 1 from R2, then multiply R2 and R1 into either register. At no time are more than two registers live, and no intermediate is stored to memory.

3. One register cannot perform b+c under the stated arithmetic rule because both non-immediate operands must be in registers simultaneously. Hence both lower and upper bounds are 2.

Original paper, page 23 · Answer key

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Minimum register spills with code motion

Code Generation & Register Allocation

Question 48, GATE 2013: Register-only processor allows at most 2 source operands and 1destination. Code: c=a+b;d=c*a;e=c+a;x=c*c;if(x>a){y=a*a;}else{d=d*d;e=e*e;}. All variables dead after segment. With only 2 registers and code motion as the only optimization, minimum spills to memory: A0, B1, C2, D3.
Question 48, GATE 2013: Register-only processor allows at most 2 source operands and 1destination. Code: c=a+b;d=c*a;e=c+a;x=c*c;if(x>a){y=a*a;}else{d=d*d;e=e*e;}. All variables dead after segment. With only 2 registers and code motion as the only optimization, minimum spills to memory: A0, B1, C2, D3.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

One spill store suffices. With registers R1, R2, start with a in R1 and b in R2. Compute R2←R1+R2 (c), store c once in spill slot S, and compute R2←R2×R2 (x). Compare R2 with R1. On the true branch compute y as R1←R1×R1. On the false branch execute: load R2←S; R2←R2×R1 (d); R2←R2×R2 (d²); load R2←S; R2←R2+R1 (e); R2←R2×R2 (e²). The computations d=c*a and e=c+a have been sunk into the false branch, where they are used, and arranged just before their respective squarings. Their former values are unused on the true branch, and all variables are dead after the segment, so this code motion preserves the required values. It performs one spill store and two reloads on the false path; reloads are not additional spills to memory. For the lower bound, after forming c the independent live values a and c already occupy both registers. Before the branch can be decided, at least one of d=c*a, e=c+a or x=c*c must be computed. If d or e is first, its result is needed for its later square while both a and c remain necessary for the other operations. If x is first, x and a are needed for the test while c is still needed on the false path. Thus the first such result forces three simultaneously needed values in a no-spill schedule, regardless of permitted reordering. A live value must be saved. Since recomputation and dead-code elimination are not allowed, zero spills is impossible and the minimum is one, option B.

The spill count in this question counts stores of live register values to memory. It does not count initial operand loads or each subsequent reload as another spill. The construction saves c once, reuses its register for x, then reloads c separately for the two false-branch calculations. Its one-store bound and the three-live-values argument together establish an exact minimum, not just an example. If arbitrary recomputation or elimination of dead code were allowed, the optimization problem would change, which is why the stated restriction matters.

Original paper, page 11 · Answer key

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Minimum registers without code motion

Code Generation & Register Allocation

Question 49, GATE 2013: For the same code c=a+b;d=c*a;e=c+a;x=c*c;if(x>a)y=a*a;else{d=d*d;e=e*e;}, all variables dead afterward. With original statement order and only register-allocation optimization, how many registers avoid spills? A3, B4, C5, D6.
Question 49, GATE 2013: For the same code c=a+b;d=c*a;e=c+a;x=c*c;if(x>a)y=a*a;else{d=d*d;e=e*e;}, all variables dead afterward. With original statement order and only register-allocation optimization, how many registers avoid spills? A3, B4, C5, D6.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Retain the printed statement order. Immediately after e=c+a and before x=c*c, a is live for the comparison and true branch, c is live for x, and d, e are live for their possible false-branch squarings. Thus {a, c, d, e} is a four-value live set, giving a lower bound of four registers. Four also suffice: R1 holds a throughout the straight-line part; R2 starts as b, is overwritten by c=a+b, then is overwritten by x=c*c once c has had its last use; R3 holds d=c*a and R4 holds e=c+a. The comparison uses R2 and R1. On the true branch, y=a*a can overwrite R1. On the false branch, d² and e² overwrite R3 and R4 respectively. No extra destination register is needed because destinations may alias source registers. Hence the exact minimum is four, option B.

Liveness asks whether a computed value will still be needed on some future path. Before x is computed, a is required for comparison, c for the square, and d/e for the possible false branch. Even though only one branch eventually executes, register allocation cannot discard d/e before the condition is known under the fixed order. Four live values therefore impose four registers. At the next step c has its final use, so x can overwrite its register and no fifth register is needed.

Original paper, page 11 · Answer key

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Registers for fixed expression-tree evaluation

Code Generation & Register Allocation

Question 36, GATE 2011: Evaluate the fixed expression tree (a-b)+(e-(c+d)) on a load-store machine. Leaves a,b,c,d,e start in memory. Binary operations require both operands in registers and leave their result in a register; no intermediate result may be stored in memory. What is the minimum number of registers needed? The tree must be evaluated as given.

(A) 2
(B) 9
(C) 5
(D) 3
Tap to enlarge
Your answer
Answer & solution

Official answer: D

A leaf needs 1 register; a binary node with equally demanding children needs one more than either, otherwise it needs the larger requirement when evaluated in the favorable order. Thus a−b needs 2; c+d needs 2; e−(c+d) also needs 2; the root combining two 2-register subtrees needs 3. Three suffice by retaining one subtree result while evaluating the other in two registers. Two cannot hold one completed child while evaluating the other fixed subtree. Hence D. This evaluates the supplied tree without algebraic reassociation; rewriting the arithmetic expression into a different tree changes the problem.

A concrete three-register schedule is: load c and d into R1,R2; replace R1 by c+d; load e into R2 and replace R2 by e-R1; then load a into R1 and b into R3, replace R1 by a-b, and add R1 to R2. The two main subtrees each need two registers to combine two operands. Whichever subtree finishes first leaves one live result while the second is evaluated, forcing a third register without spilling or reassociation.

Original paper, page 10 · Answer key

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Minimum registers without spilling

Code Generation & Register Allocation

Question 37, GATE 2010: All operations take operands from registers. What is the minimum number of registers needed to execute this program without spilling?
a=1; b=10; c=20;
d=a+b; e=c+d; f=c+e;
b=c+e; e=b+f; d=5+e;
return d+f;
Options: (A) 2; (B) 3; (C) 4; (D) 6.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Immediately after c=20, a,b,c are all live because a and b are needed for d=a+b while c is needed later. At least three registers are therefore necessary. Three suffice: put a,b,c in registers r1,r2,r3; overwrite r1 with d=a+b, then with e=c+d; overwrite r2 with f=c+e; overwrite r3 with b=c+e after c dies; overwrite r1 with e=b+f and then d=5+e. The final d+f uses r1 and r2. Thus 3, B, under the usual model allowing a destination to reuse a dead operand register.

One possible register state trace is (a,b,c)→(d,b,c)→(e,b,c)→(e,f,c)→(e,f,b)→(e,f,b)→(d,f,b), where dead values may be overwritten. At the middle expression f=c+e, both c and e must remain available for the following b=c+e, so the third register is still useful. The lower-bound argument assumes executing the given operations, rather than constant-folding the entire straight-line program.

Original paper, page 9

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Register allocation and MOV count in a basic block

Code Generation & Register Allocation

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
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. Track the unusual operand order: SUB e,R2 computes e−R2, not R2−e. Under expression-preserving generation, retain t1 in one register while computing t2 and t3 in the other. The scope of permitted algebraic rewrites affects the literal minimum.

2. Derivation and interpretation. For expression-preserving code generation, three MOVs suffice: MOV a,R1; ADD b,R1; MOV c,R2; ADD d,R2; SUB e,R2; SUB R1,R2; MOV R2,t4. The specified source-minus-destination semantics gives R1=t1, R2=t2 then t3=e−t2 then t4=t1−t3. There are two loads to initialize the two independently computed subexpressions and one store, yielding the intended option B (3). This minimum presumes the usual task of generating code for the given expression structure. If algebraic reassociation is allowed, two MOVs suffice: MOV c,R1; ADD d,R1; SUB e,R1; SUB a,R1; ADD b,R1; MOV R1,t4. This computes t4=a−(e−(c+d))+b. Furthermore, if arbitrary arithmetic instructions may initialize a register, SUB R1,R1 makes zero; replacing the initial MOV c,R1 by SUB R1,R1; ADD c,R1 leaves only the final MOV. Hence the literal unrestricted minimum is not 3, and the scope assumption must be disclosed; B is an intended, conditional answer rather than an unconditional theorem.

Original paper, page 13

1 mark · MCQ
Not in GATE 2027 syllabusReport issue

Evaluation order with one accumulator

Code Generation & Register Allocation

Question 10, GATE 2004: For E→E1−E2, generate code for a CPU with only one user register. Subtraction requires its first operand in that register; E1 and E2 have no common subexpression. Which evaluation order gives the shortest code?
Options:
(A) Evaluate E1 first
(B) Evaluate E2 first
(C) Necessarily interleave E1 and E2 evaluation
(D) Evaluation order is of no consequence
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Evaluate E2 first and save its value in a temporary memory location. Then evaluate E1, leaving the required first operand in the sole register, and subtract the saved E2.

2. Evaluating E1 first would require saving it, evaluating/storing E2, then reloading E1 before subtraction. Thus E2-first, B, avoids an extra reload in the stated accumulator-style model, assuming both expressions require evaluation and their order is free of side-effect constraints.

3. Teaching check: The final subtraction needs register=E1 and memory temporary=E2. E2-first naturally reaches that arrangement after one save. E1-first must preserve E1 across E2’s evaluation, then reload it, adding a memory operation. This argument assumes unrestricted evaluation order and nontrivial expression evaluations.

Original paper, page 2

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

One-register evaluation and precedence

Code Generation & Register Allocation

Question 4.9, GATE 1997: For expression prefix (a*b)*c op…where op is+, *, or exponentiation, which operators permit the accumulated product to remain in one register?
Tap to enlarge
Your answer
Answer & solution

Answer: A: + or * under the standard left-to-right accumulator model (no official key was published)

After computing a*b, the single accumulator holds that intermediate value. If op is+or*, ordinary precedence and left associativity allow completing the multiplication by c before moving to the following operation; the accumulator can be reused without spilling a*b. If op is exponentiation, the c-power subexpression binds more tightly and normally must be computed before multiplying by the held a*b, creating a second live intermediate. Thus A in the stated compiler-evaluation model. The omitted suffix and machine instruction set are not fully specified; recomputation or different schedules can change storage requirements, so this is not an architecture-independent impossibility theorem.

Original paper, page 4

5 marks · Descriptive
Not in GATE 2027 syllabusReport issue

A complete one-register translation scheme

Code Generation & Register Allocation

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.
Tap to enlarge
Answer & solution

Answer: Use synthesized code/place attributes and a fresh memory temporary for each addition; assign by LOAD expression.place; STORE target. (no official key was published)

Give every E and T synthesized attributes code (an instruction list) and place (a memory address containing its value). An identifier token has its declared address id.addr. Let newtemp() allocate a fresh, non-aliasing memory cell and || concatenate instruction lists. The following bottom-up semantic rules form a complete syntax-directed definition.

T→id: T.code=[]; T.place=id.addr.
T→(E): T.code=E.code; T.place=E.place.
E→T: E.code=T.code; E.place=T.place.
E→E1+T: choose t=newtemp(); E.code=E1.code || T.code || [LOAD E1.place, ADD T.place, STORE t]; E.place=t.
A→id:=E: A.code=E.code || [LOAD E.place, STORE id.addr].

The invariant is that executing a subtree's code leaves its value in its designated memory cell. Identifier leaves already satisfy this. Parentheses and E→T preserve it. In an addition, both operands are available in memory after their code has run; LOAD fetches the left one and ADD combines the right one without needing a second register. STORE preserves the result for enclosing expressions. Fresh temporaries prevent a right subtree from overwriting the saved left value. The final assignment changes the target only after the whole expression has been evaluated, so x:=x+y also works.

For x:=a+(b+c), first emit LOAD b; ADD c; STORE t1. Then LOAD a; ADD t1; STORE t2. Finally LOAD t2; STORE x. This deliberately simple scheme may contain redundant loads/stores, but optimization is not required. It uses exactly the three allowed instruction types and handles arbitrary nesting, unlike a scheme that leaves an unprotected partial result only in R.

Original paper, page 7

2 marks · Other
Not in GATE 2027 syllabusReport issue

Registers with minimum operand-memory traffic

Code Generation & Register Allocation

Question 1(xi), GATE 1991: Evaluate (a+b)*c−d/e**f on a two-address machine whose operands may be registers or memory. Minimize operand memory accesses and give the required registers. ** denotes exponentiation.
Question 1(xi), GATE 1991: Evaluate (a+b)*c−d/e**f on a two-address machine whose operands may be registers or memory. Minimize operand memory accesses and give the required registers. ** denotes exponentiation.
Question 1(xi), GATE 1991: Evaluate (a+b)*c−d/e**f on a two-address machine whose operands may be registers or memory. Minimize operand memory accesses and give the required registers. ** denotes exponentiation.
Tap to enlarge
Answer & solution

Answer: 2 registers and 6 input reads, excluding final result storage (no official key was published)

Assume six independent input values initially reside in memory, arithmetic allows a memory source and register destination, and exponentiation is an available operation. Read e into R1 and compute R1:=R1**f; read d into R2 and compute R2:=R2/R1. Reuse R1 for a, then add b and multiply by c; finish R1:=R1−R2. Each of a, b, c, d, e, f is read once, achieving the six-read lower bound. Two registers retain one partial result while forming the other; one register would require a spill or destructive memory temporary and extra traffic. A required final output store adds one access; instruction fetches are not operand accesses.

Original paper, page 1