GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Solve live variables over control-flow graphs

3 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Compute USE/DEF, propagate OUT as successor-IN union and IN as USE union OUT-minus-DEF to a fixed point. Preserve entry versus exit queries and back edges; programs differ.

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 · 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 · 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