GATE / CS ARCHIVE

SUBJECT COLLECTION

Compiler Design · Code Optimization & Data-Flow Analysis

15 questions, newest first

GATE CSE has asked 15 questions on Code Optimization & Data-Flow Analysis (Compiler Design) from 2006 to 2026, in 12 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 1.2 marks per 100-mark paper on average, against 0.7 since 2003. Since 2014 the questions have been 10 MCQ and 2 NAT. 3 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
2 marks · MCQ
Report issue

Available expressions at a control-flow merge

Code Optimization & Data-Flow Analysis

Question 42, GATE 2026: The flow is Entry→B1→either B2 or B3→B4→B5→Exit. B1: a=b+i. B2: a=g*k; f=d−f; a=c*4. B3: t=g*k; b=c+m. B4: x=g*k; y=b+i. B5: z=c+m. All variables are integers. Find the redundant expression sets in B4 and B5.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Givens and goal

The flow is Entry→B1→either B2 or B3→B4→B5→Exit. B1: a=b+i. B2: a=g*k; f=d−f; a=c*4. B3: t=g*k; b=c+m. B4: x=g*k; y=b+i. B5: z=c+m. All variables are integers. Find the redundant expression sets in B4 and B5.

Approach

An expression is available only if it has already been computed on every incoming path and neither operand has been reassigned since.

Step-by-step solution

1. The control-flow shape is a diamond followed by a tail:

    B1 ──→ B2 ──┐
      └──→ B3 ──┴──→ B4 ──→ B5

At the B4 merge, availability uses the intersection of the incoming expression sets, not their union.
2. B1 computes b+i. Along B2 neither b nor i is assigned, so this expression remains available there. Along B3, however, b is overwritten by c+m. That kills the old b+i value. Hence b+i is not available on every path to B4 and y=b+i is not globally redundant.
3. Both B2 and B3 compute g*k, and neither subsequently changes g or k. Thus g*k is available along both incoming paths. Its evaluation in x=g*k is redundant. B2 later overwrites a, but that changes the destination holding a previous result, not the operands g and k; a transformation may preserve the value in a temporary.
4. B3 computes c+m, but B2 does not. B4 computes neither c+m nor anything proving its value was previously computed on the B2 path. Therefore z=c+m in B5 is not redundant on all paths.
5. The required sets are B4:{g*k}, B5:empty, option D. Option A wrongly keeps b+i; B wrongly treats a computation on only one branch as universally available; C also incorrectly keeps b+i.

In particular a=c*4 assigns a, not c; the target variable matters when deciding whether an expression is killed.

Original paper, page 31 · Answer key

2 marks · MCQ
Report issue

Live variables across a control-flow loop

Code Optimization & Data-Flow Analysis

Question 45, GATE 2026: Blocks execute B1: a=b+c; B2: d=a+e; B3: e=a+f; B4: g=d+e. Edges are entry→B1, B1→B2, B1→B3, B2→B4, B3→B1, B4→exit. No variable is live after exit. Candidate exit sets (B1,B2,B3,B4) are A ({a,b,c,e,f},{d,e},{b,c,e,f},∅); B (∅,{d,e},{a,c,f},∅); C ({a,b,c,e,f},{d,e},{c,e,f},∅); D (∅,{d,e,f},{a,b,c,e,f},∅).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

Blocks execute B1: a=b+c; B2: d=a+e; B3: e=a+f; B4: g=d+e. Edges are entry→B1, B1→B2, B1→B3, B2→B4, B3→B1, B4→exit. No variable is live after exit. Candidate exit sets (B1,B2,B3,B4) are A ({a,b,c,e,f},{d,e},{b,c,e,f},∅); B (∅,{d,e},{a,c,f},∅); C ({a,b,c,e,f},{d,e},{c,e,f},∅); D (∅,{d,e,f},{a,b,c,e,f},∅).

Approach

Solve backward liveness equations to a fixed point, including the loop edge.

Step-by-step solution

1. For each block, OUT is the union of successor IN sets; IN=USE∪(OUT−DEF). Here USE/DEF are B1:{b,c}/{a}, B2:{a,e}/{d}, B3:{a,f}/{e}, and B4:{d,e}/{g}.
2. OUT4=∅, so IN4={d,e}. Hence OUT2={d,e} and IN2={a,e}∪({d,e}−{d})={a,e}.
3. The loop equations are OUT3=IN1, IN3={a,f}∪(IN1−{e}), OUT1=IN2∪IN3, and IN1={b,c}∪(OUT1−{a}). Starting empty and propagating produces the least fixed point IN1={b,c,e,f} and IN3={a,b,c,f}.
4. Substitute back to check: OUT1={a,e}∪{a,b,c,f}={a,b,c,e,f}; removing a and adding b,c indeed gives IN1={b,c,e,f}. OUT3=IN1={b,c,e,f}; removing e and adding a,f indeed gives IN3={a,b,c,f}.
5. The requested exit sets are therefore OUT1={a,b,c,e,f}, OUT2={d,e}, OUT3={b,c,e,f}, OUT4=∅, matching A. In particular b stays live through B3 because the loop can return to B1 and read b before any redefinition.
6. The source assignments and all arrows were inspected visually. Ignoring the back edge would incorrectly remove values needed on a later loop iteration.

Original paper, page 31 · Answer key

2 marks · NAT
Report issue

Finding leaders and basic blocks

Code Optimization & Data-Flow Analysis

Question 52, GATE 2025: Partition this three-address code into maximal basic blocks. 1001 i=1; 1002 j=1; 1003 t1=10*i; 1004 t2=t1+j; 1005 t3=8*t2; 1006 t4=t3−88; 1007 a[t4]=0.0; 1008 j=j+1; 1009 if j≤10 goto 1003; 1010 i=i+1; 1011 if i≤10 goto 1002; 1012 i=1; 1013 t5=i−1; 1014 t6=88*t5; 1015 a[t6]=1.0; 1016 i=i+1; 1017 if i≤10 goto 1013. How many blocks?
Tap to enlarge
Answer & solution

Official answer: 6

Approach: Mark every leader: the first instruction, every jump target, and every instruction immediately following a jump when present in the supplied code.
The first instruction makes 1001 a leader. Branch targets make 1003,1002,1013 leaders. The instructions following branches at 1009 and 1011 are 1010 and 1012, so they are leaders too. The branch at 1017 is the last supplied instruction; there is no following instruction to create an extra block.
Sorting the distinct leaders gives 1001,1002,1003,1010,1012,1013. Each block extends to just before the next leader, or to the end: [1001], [1002], [1003–1009], [1010–1011], [1012], [1013–1017]. There are six blocks.
A basic block has one entry and no internal branch out before its end. This explains why even a single assignment at 1002 or 1012 can form its own block: the next instruction is independently a branch target. Counting loops or conditionals alone would not identify the correct partition.

Original paper, page 52 · Answer key

2 marks · MCQ
Report issue

Partition code into basic blocks

Code Optimization & Data-Flow Analysis

Question 39, GATE 2024: The code is L1 t1=−1; L2 t2=0; L3 t3=0; L4 t4=4*t3; L5 t5=4*t2; L6 t6=t5*M; L7 t7=t4+t6; L8 t8=a[t7]; L9 if t8<=max goto L11; L10 t1=t8; L11 t3=t3+1; L12 if t3<M goto L4; L13 t2=t2+1; L14 if t2<N goto L3; L15 max=t1. Find (number of basic blocks, instructions in largest block). A (6,6); B (6,7); C (7,7); D (7,6).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Approach: A basic block is a maximal straight-line instruction sequence with entry only at its first instruction and transfer out only at its last. Identify every leader before counting.
1. The first instruction L1 is a leader. Branch targets L11, L4 and L3 are leaders because execution can enter directly at them.
2. Instructions immediately after conditional branches are also leaders: L10 follows L9, L13 follows L12, and L15 follows L14. These are the fall-through paths.
3. Sort the distinct leaders: L1, L3, L4, L10, L11, L13, L15. Each block runs from one leader through the instruction just before the next leader.
4. The blocks are [L1–L2], [L3], [L4–L9], [L10], [L11–L12], [L13–L14], [L15]. Their lengths are 2,1,6,1,2,2,1, totaling all 15 instructions.
Answer: D, seven blocks with largest size six. The terminating branch L9 belongs to its block; a branch target such as L3 begins a new block even when the preceding instruction falls through to it.

Original paper, page 25 · Answer key

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 37, GATE 2023: The control-flow graph has B1: i=m−1;j=n;a=10; B2: i=i+1;j=j−1; B3: a=20; B4: i=a+1. Edges are entry→B1→B2, B2→B3, B2→B4, B3→B4, B4→B2, and B4→exit. Find the live-variable set at each block exit; no values are used after exit.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Liveness means a current value can be read along some future path before redefinition. B4 computes i from a, then can return to B2, where i and j are read; a can also survive to another B4. Thus OUT(B4)={a, i, j}. Before B4 the old i is killed, so IN(B4)={a, j}, yielding OUT(B3)={a, j}. B2 can reach B4 directly or through B3, so OUT(B2)={a, j}; its updates read i, j, yielding IN(B2)={a, i, j}=OUT(B1). Therefore D.

Teaching detail. Use OUT(B)=the union of IN over successor blocks and IN(B)=USE(B) union (OUT(B)−DEF(B)). Here USE(B2)={i,j}, DEF(B2)={i,j}, USE(B3)=∅, DEF(B3)={a}, USE(B4)={a}, DEF(B4)={i}. The loop requires a fixed point rather than a single backward pass. Crucially j remains live through B4 because the next B2 reads it; old i is killed inside B4 but the newly assigned i is live afterward. Values m,n are used only in B1 and are not live at its exit.

Original paper, page 37 · Answer key

2 marks · NAT
Report issue

Code Optimization & Data-Flow Analysis

Question 50, GATE 2021: A compiler represents this basic block by a common-subexpression DAG: a=b+c; e=a+1; d=b+c; f=d+1; g=e+f;. How many DAG nodes are needed? The symbol in the additions to a and d is the constant digit 1.
Tap to enlarge
Answer & solution

Official answer: 6

The code uses the constant 1, not a variable i. A common-subexpression DAG has three leaves b, c, 1. One addition node b+c is labeled by both a and d. A second addition node (b+c)+1 is labeled by both e and f. The final addition node adds that second node to itself and is labeled g. Hence there are 3 leaves plus 3 operation nodes, totaling 6. Variable labels are not separate DAG nodes in this representation.

Name the six nodes: n1=b, n2=c, n3=constant 1, n4=+(n1,n2) labeled a,d, n5=+(n4,n3) labeled e,f, n6=+(n5,n5) labeled g. The code does not change b or c, so the two b+c expressions really share one node. Repeated variable names attached as labels are not new values/nodes. This is common-subexpression DAG construction, not a syntax tree that duplicates every appearance or algebraic strength reduction to multiplication.

Original paper, page 39 · Answer key

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 30, GATE 2021: Code: z=x+3+y->f1+y->f2; for(i=0;i<200;i=i+2){if(z>i){p=p+x+3;q=q+y->f1;}else{p=p+y->f2;q=q+x+3;}}. y points to a heap struct; x,y,z,p,q,i reside in registers. After common subexpression elimination, count additions and field dereferences. A (403,102); B (203,2); C (303,102); D (303,2).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Compute and reuse x+3, y->f1, and y->f2. The initial expression then uses 3 additions and 2 dereferences. There are 100 loop iterations; either branch needs one addition for p and one for q, and the increment i=i+2 needs one more. Thus total additions 3+100·3=303, while both dereferenced fields remain reusable because the loop does not modify them. Answer D, under the specified CSE model.

An optimized schematic is u=x+3;v=y->f1;w=y->f2;z=u+v+w; followed by branches (p+=u;q+=v) or (p+=w;q+=u). There are 100 iterations because i runs 0,2,…,198. CSE does not erase the loop-counter addition or the two updates in either branch. The count assumes the problem's arithmetic regrouping/CSE model and no intervening field writes or aliasing stores.

Original paper, page 71 · Answer key

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 38, GATE 2021: USE(S) means variables used in S; IN(S),OUT(S) are live variables on entry/exit. Basic block is S1 followed by S2. A OUT(S1)=IN(S2); B OUT(S1)=IN(S1)∪USE(S1); C OUT(S1)=IN(S2)∪OUT(S2); D OUT(S1)=USE(S1)∪IN(S2).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Inside a basic block S1 has S2 as its unique immediate successor. Values live just after S1 are exactly those live just before S2, so OUT(S1)=IN(S2), A. Uses at S1 do not automatically remain live afterward, and a definition in S2 can kill a value that is live after S2.

The general equations are OUT(S)=union of IN of immediate successors, and IN(S)=USE(S)∪(OUT(S)−DEF(S)). A basic block has no branch between S1 and S2, reducing the first equation to A. For example S1 reads x then S2 overwrites x: x can be used at S1 without needing to remain live after it, refuting formulas that force USE(S1) into OUT(S1).

Original paper, page 77 · Answer key

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 46, GATE 2015: A variable is live at statement Si if some reachable use Sj has no intervening assignment, including Si and Sj. CFG: block 1 p=q+r; s=p+q; u=s*v; branches to block 2 v=r+u or block 3 q=s*u; both go to block 4 q=v+r; block 4 loops to block 1. Which are live at both block 2 and block 3 entry? A p,s,u; B r,s,u; C r,u; D q,v.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Use the old value test: a value matters only if a path reads it before replacement. At block 2, r and u are read immediately; old v is killed by v=r+u, old q by block 4, and p,s by block 1 before future use. At block 3, s,u are immediate reads and v,r remain needed at block 4. Taking the intersection of {r,u} and {s,u,v,r} leaves {r,u}. Liveness is backward data flow and uses possible paths, not just values read in the next line.

Solution: At entry to block 2, r and u are used by v=r+u; old v is overwritten and old q is overwritten in block 4. At entry to block 3, s and u are used by q=s*u, while v and r are needed by block 4. Thus live-in sets are{r, u} and{s, u, v, r}; their intersection is{r, u}, C. Values overwritten in block 1 before any later use need not be added.

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 17, GATE 2014: Which compiler statement is false? A: A basic block is entered at its beginning and exited at its end. B: Available-expression analysis supports common-subexpression elimination. C: Live-variable analysis supports dead-code elimination. D: Replacing x=4*5 by x=20 is common-subexpression elimination.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

1. Method and setup. Common-subexpression elimination reuses a previously computed expression whose operands have not changed. Here no prior computation is being reused: a constant expression is evaluated during compilation. Available-expression and liveness analyses instead answer whether a value is reusable and whether a computed result will be needed.

2. Work through the result and its assumptions. Replacing the compile-time arithmetic 4×5 by 20 is constant folding, not common-subexpression elimination. The other statements describe standard basic-block and dataflow uses. Thus the false statement is D.

Original paper, page 11 · Answer key

1 mark · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 17, GATE 2014: Which is a purpose of intermediate code in a compiler? A simplify parsing and semantic analysis; B improve error recovery/reporting; C increase reuse of a machine-independent optimizer across compilers; D improve register allocation.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step 1: Separate a compiler into a source-language front end, an optimizer and a target-machine back end. A shared intermediate representation gives the middle portion a common input/output language. Step 2: The same machine-independent optimization passes can then be used for several source languages or machine targets. This directly explains C. Step 3: Parsing and semantic analysis are primarily front-end tasks, and register allocation is a later target-related task. Intermediate code may assist a design, but the other choices do not state its principal reuse motivation.

Original paper, page 56 · Answer key

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 34, GATE 2014: The basic block executes a=b+c; c=a+d; d=b+c; e=d−b; a=e+b. What are the minimum DAG node and edge counts? A (6,6); B (8,10); C (9,12); D (4,4).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A with algebraic simplification

Step 1: Preserve old values when tracking assignments: let the input leaves be b0,c0,d0. The first three operations create u=b0+c0, v=u+d0, w=b0+v. The current c is v and current d is w. Step 2: Algebraically, e=w−b0=(b0+v)−b0=v, and final a=e+b0=v+b0=w. Therefore e shares v's node and a shares w's node. Step 3: The minimized DAG has three input leaves and three binary addition nodes: six nodes. Each operation has two incoming operand edges, giving six edges, A. Without cancellation, a purely syntactic DAG gives eight nodes and ten edges. The keyed minimum assumes valid algebraic identities, not unrestricted floating-point cancellation.

Original paper, page 60 · Answer key

1 mark · MCQ
Report issue

Intermediate-code optimization portability

Code Optimization & Data-Flow Analysis

Question 12, GATE 2008: Why are some code optimizations performed on intermediate code?

(A) They enhance compiler portability to other target processors
(B) Program analysis is more accurate on intermediate code than machine code
(C) Dataflow-analysis information cannot otherwise be used for optimization
(D) Front-end information cannot otherwise be used for optimization
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Intermediate code abstracts away many machine-specific details, allowing one optimization pass to serve several target back ends.
2. This reuse makes it easier to retarget the compiler, which is the portability benefit in A.
3. B claims a universal analysis-accuracy advantage that does not follow. C and D incorrectly say dataflow/front-end information cannot otherwise be used; such information can also inform other compilation stages.

Original paper, page 3

2 marks · MCQ
Report issue

Common-subexpression reuse across an array store

Code Optimization & Data-Flow Analysis

Question 55, GATE 2006: Consider C functions:
int work 1(int *a,int i,int j) {
 int x=a[i+2]; a[j]=x+1; return a[i+2]-3;
}
int work 2(int *a,int i,int j) {
 int t1=i+2; int t2=a[t1]; a[j]=t2+1; return t2-3;
}
S1: work 2 always gives the same output and effect on state as work 1.
S2: the transformations always reduce CPU time.
Which truth values apply?
Options:
(A) S1 false, S2 false
(B) S1 false, S2 true
(C) S1 true, S2 false
(D) S1 true, S2 true
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. work 1 reads a[i+2], writes a[j], then reads a[i+2] again in its return expression. work 2 saves the first value and reuses it after the store. If j=i+2, the store changes the location that work 1 rereads, so the functions need not return the same value; S1 is false.

2. Compiler transformations also do not universally improve runtime on every target/input, and this transformation is not even semantics-preserving without a non-alias condition. S2 is false.

3. Thus A.

4. For i=0, j=2, initial a[2]=5, work 1 returns 3 whereas work 2 returns 2.

5. Teaching check: Take i=0,j=2,a[2]=5. Both functions store 6 into a[2], but work 1 then rereads 6 and returns 3, while work 2 reuses saved 5 and returns 2. Reusing a load across a potentially aliasing store is not a valid unconditional common-subexpression transformation.

Original paper, page 8

2 marks · MCQ
Report issue

Identifying unavailable loop optimization

Code Optimization & Data-Flow Analysis

Question 60, GATE 2006: Consider C code (with defined arithmetic and x,y live after the fragment):
for (i=0;i<n;i++) {
 for (j=0;j<n;j++) {
  if (i%2) { x+=(4*j+5*i); y+=(7+4*j); }
 }
}
Which optimization claim is not supported by this fragment?
Options:
(A) It contains loop-invariant computation
(B) Common-subexpression elimination is possible
(C) Strength reduction is possible
(D) Dead-code elimination is possible
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Within the inner j loop, i%2 and 5*i are invariant. The expression 4*j occurs in both updates and can be shared.

2. The products by loop indices can use strength reduction through induction-variable increments. However x and y are updated and may be used later; nothing in the snippet establishes that those results are dead.

3. Therefore the claim of available dead-code elimination is unsupported and is the intended false statement, D. This uses the ordinary compiler-question assumption that program behavior is defined and x,y remain live afterward.

4. Teaching check: An expression can be expensive and still necessary. Dead-code elimination requires showing that its computed value never affects observable behavior, which is not established for x or y. In contrast, the repeated arithmetic patterns are visible directly within the loops.

Original paper, page 9