GATE / CS ARCHIVE

GATE CSE 2025 SET 1 / QUESTION 52

Finding leaders and basic blocks

Compiler Design · Code Optimization & Data-Flow Analysis · Full GATE CSE 2025 Set 1 paper

GATE 2025 Set 1, Q52 2 marks · NAT
Report issue

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